继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Sqlite3+EF6踩的坑

函数式编程
关注TA
已关注
手记 54
粉丝 14
获赞 30

摘要

最近在用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>


好了,现在我们插入一条数据

            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特性显示指定表名称。

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP