Sqlite3+EF6踩的坑

摘要

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

一個例子

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

安裝後,你會發如今app.config中,添加關於sqlite的配置。sql

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

    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.configapp

<?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>

關於其中修改的部分能夠參考這篇文章ide

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

好了,如今咱們插入一條數據測試

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

出錯了ui

錯誤信息spa

{"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

相關文章
相關標籤/搜索