Sqlite3+EF6踩的坑


摘要

最近在用winform,有些數據需要本地存儲,所以想到了使用sqlite這個文件數據庫。在使用Nuget安裝sqlite的時候,發現會將Ef也安裝上了,所以想着使用EF進行數據的操作吧,所以這就來了,各種坑。

一個例子

首先使用Nuget安裝sqlite。安裝成功后如圖所示:

安裝后,你會發現在app.config中,添加關於sqlite的配置。

添加測試類以及數據上下文。

    public class Person
    {
        [Key]
        public Guid Id { set; get; }
        public string Name { set; get; }
    }
   public class RetailContext : DbContext
    {
        public RetailContext()
            : base("SqliteTest")
        {
        }      public DbSet<Person> Persons { set; get; }

    }

最終的app.config

<?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="SqliteTest" connectionString="Data Source=E:\retail.db" providerName="System.Data.SQLite.EF6" /> </connectionStrings> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v13.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> <!-- 1. Solves SQLite error of "Unable to find the requested .Net Framework Data Provider."--> <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> </providers> </entityFramework> <system.data> <DbProviderFactories> <remove invariant="System.Data.SQLite.EF6" /> <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
type
="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/> <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>

關於其中修改的部分可以參考這篇文章

http://www.cnblogs.com/lifeil/p/3944334.html

好了,現在我們插入一條數據

            using (Retail.Data.RetailContext context = new Data.RetailContext())
            {                             
                context.Persons.Add(new Person { Id = Guid.NewGuid(), Name = "wolfy1" });
                context.SaveChanges();
            }

出錯了

錯誤信息

{"SQL logic error or missing database\r\nno such table: People"}

哪來的People表?

然后就是各種搜索,發現sqlite在創建表的時候,默認是使用實體類的復數形式進行創建,person的復數不是persons么(對開發人員來說),而sqlite不按套路出牌,人家認為Person的復數就是People,相當無語了,欺負我英語不好么?

那我們如果使用特性指定數據表的名字行不行,修改實體類

    [Table("Persons")]
    public class Person
    {
        [Key]
        public Guid Id { set; get; }
        public string Name { set; get; }
    }

結果成功了,看來在使用sqlite和ef6的時候,需要指定table特性,並設置對應的數據表名稱。

那我們看另外一個,我們添加一個Student類

    public class Student
    {
        [Key]
        public Guid Id { set; get; }
        public string Name { set; get; }
    }

注意,這里並沒有設定數據表,沒有為其添加Table特性。

            using (Retail.Data.RetailContext context = new Data.RetailContext())
            {
                context.Students.Add(new Student { Id = Guid.NewGuid(), Name = "wolfy1" });
                context.SaveChanges();
            }

結果

這又成功了。讓人搞不懂,在使用ef+mysql,或者sqlserver的時候,默認是使用數據上下文中指定的屬性名稱作為數據表名稱的。但sqlite不行,如果不指定Table特性設置數據表名稱,就欺負你英語不好的了,怎么地了?

 public class RetailContext : DbContext
    {
        public RetailContext()
            : base("SqliteTest")
        {
        }
        public DbSet<Student> Students { set; get; }
        public DbSet<Person> Persons { set; get; }

    }

總結

在EF6+SQLITE3的時候,注意指定對應實體類對應的Table特性顯示指定表名稱。

資料

http://blog.csdn.net/make1828/article/details/40071455


免責聲明!

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



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