EF6 在 SQLite中使用備忘

== 菜鳥級選手試驗在EF6中使用Sqlite,零EF基礎,少許Sqlite基礎。通過斷斷續續的很長時間 - _ -!sql

>>鏈接數據庫

1. 安裝c#

   使用目前最新版本EF6.1,Sqlite1.0.93.0。直接NuGet安裝:ide

   

2. 配置測試

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version= "1.0" encoding= "utf-8" ?>
<configuration>
   <configSections>
     <section name= "entityFramework" type= "System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission= "false" />
     <!-- For more information on Entity Framework configuration, visit http: //go.microsoft.com/fwlink/?LinkID=237468 -->
   </configSections>
   <startup>
     <supportedRuntime version= "v4.0" sku= ".NETFramework,Version=v4.5" />
   </startup>
   <connectionStrings>
     <add name= "SqlliteEF6" connectionString= "Data Source=Data\EF6.db" providerName= "System.Data.SQLite" />    </connectionStrings>
   <entityFramework>
     <providers>
       <provider invariantName= "System.Data.SQLite" type= "System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
       <provider invariantName= "System.Data.SQLite.EF6" type= "System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
     </providers>
     <defaultConnectionFactory type= "System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework" >
       <parameters>
         <parameter value= "v11.0" />
       </parameters>
     </defaultConnectionFactory>
   </entityFramework>
   <system.data>
     <DbProviderFactories>
       <remove invariant= "System.Data.SQLite" />
       <add name= "SQLite Data Provider" invariant= "System.Data.SQLite" description= "Data Provider for SQLite" type= "System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
       <remove invariant= "System.Data.SQLite.EF6" />
       <add name= "SQLite Data Provider (Entity Framework 6)" invariant= "System.Data.SQLite.EF6" description= ".Net Framework Data Provider for SQLite (Entity Framework 6)" type= "System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
     </DbProviderFactories>
   </system.data>
</configuration>

 3. 設置數據庫ui

    EF中數據庫操做繼承DBContext。須要指定數據鏈接,名稱和config配置一致,不然默認使用Sqlserver。this

    SQLite能夠重載建立。spa

 

1
2
3
4
5
6
7
8
9
10
11
public  class  EF6Context : DbContext
     {
         public  EF6Context( string  databaseName =  "SqlliteEF6" )
             base (databaseName)
         {
         }
 
         public  DbSet<User> Users {  set get ; }
 
         protected  override  void  OnModelCreating(DbModelBuilder modelBuilder)
         {}
     }
 

 

  

>>注意事項code

1. Sqlite中不能自動建立數據庫和新建表,須要額外的操做orm

>> 數據類型插入獲取

1. 自增ID

   Sqlite中須要設置AUTOINCREMENT,以下:

1
Id INTEGER PRIMARY KEY AUTOINCREMENT,

須要引用System.ComponentModel.DataAnnotations,System.ComponentModel.DataAnnotations.Schema,類中寫明

1
2
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int64 Id { get ; set ; }

2. 限定Table

  默認數據庫獲取爲DBContext中定義的名稱,限定對應的Table,須要在類上指明,以下:

1
2
3
[Table( "User" )]
public class User
{...

3. 枚舉型

   目前直接能使用,注意須要在定義表時限定不能爲NULL,不然爲NULL時獲取會報錯。定義時以下定義:

1
public enum TestENUM : long { A, B, C };

 

4. 其它

   目前SQlite中支持浮點數、時間、二進制數據、字符串等。建立表示例:

1
2
3
4
5
6
7
8
9
NorthwindContext context = new NorthwindContext();
            string sql = @" CREATE TABLE User (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    Name varchar (20),
    Time timestamp,
    Data blob,
    Val REAL,
    TestE INTEGER);" ;
            context.Database.ExecuteSqlCommand(sql, new object [1]);

   定義類示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[Table( "User" )]
public class User
{
     public enum TestENUM : long { A, B, C };
 
     [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
     public Int64 Id { get ; set ; }
     [StringLength(30)]
     public string Name { get ; set ; }
 
     public byte [] Data { get ; set ; }
 
     public double Val { get ; set ; }
 
     public DateTime Time { get ; set ; }
     public TestENUM TestE { get ; set ; }  
}

  

1
2
3
4
5
6
7
8
9
10
11
public  class  EF6Context : DbContext
     {
         public  EF6Context( string  databaseName =  "SqlliteEF6" )
             base (databaseName)
         {
         }
 
         public  DbSet<User> Users {  set get ; }
 
         protected  override  void  OnModelCreating(DbModelBuilder modelBuilder)
         {}
     }
 

 

     調用示例:

 

1
2
3
NorthwindContext context = new NorthwindContext();
context.Users.Add( new User() { Data = new byte [] { 1, 2, 3, 4 }, Name = "aa22" , Time = DateTime.Now, Val = 2.2,  TestE    = User.TestENUM.B });
context.SaveChanges();

 

1
2
3
NorthwindContext context = new NorthwindContext();
context.Users.OrderBy(c => c.Name).Load();
this .dataGrid.ItemsSource = context.Users.Local;

    以上代碼測試正常。源碼下載

 

 

 



相關文章
相關標籤/搜索