EF6 在 SQLite中使用備忘


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

>>連接

1. 安裝

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

   

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. 設置數據庫

    EF中數據庫操作繼承DBContext。需要指定數據連接,名稱和config配置一致,否則默認使用Sqlserver。

    SQLite可以重載創建。

 

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. Sqlite中不能自動創建數據庫和新建表,需要額外的操作

>> 數據類型插入獲取

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;

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

 

 

 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM