实体框架6与SQLite 3代码第一 - 不会创建表 [英] Entity Framework 6 with SQLite 3 Code First - Won't create tables

查看:97
本文介绍了实体框架6与SQLite 3代码第一 - 不会创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用NuGet的最新版本的EF6和SQLite。我终于得到了app.config文件在Stackoverflow上的一些有用的帖子后工作。现在的问题是尽管数据库是没有创建表。



我的app.config:

 <?xml version =1.0encoding =utf-8?> 
< configuration>
< configSections>
<! - 有关实体框架配置的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
< section name =entityFrameworktype =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089requirePermission =false/> ;
< / configSections>
< startup>
< supportedRuntime version =v4.0sku =。NETFramework,Version = v4.5/>
< / startup>
< entityFramework>
< providers>
< 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/>
< add name =SQLite数据提供者
invariant =System.Data.SQLite
description =。SQLite的Net Framework数据提供者
type =系统.Data.SQLite.SQLiteFactory,System.Data.SQLite/>
< remove invariant =System.Data.SQLite.EF6/>
< add name =SQLite数据提供程序(实体框架6)
invariant =System.Data.SQLite.EF6
description =。SQLite的Net Framework数据提供程序(实体框架6)
type =System.Data.SQLite.EF6.SQLiteProviderFactory,System.Data.SQLite.EF6/>
< / DbProviderFactories>
< /system.data>
< connectionStrings>
< add name =MyDBContext
connectionString =Data Source = | DataDirectory | MyDB.sqlite
providerName =System.Data.SQLite/>
< / connectionStrings>
< / configuration>

我的简单测试程序:

  class Program 
{
static void Main(string [] args)
{
using(var db = new MyDBContext())
{
db.Notes.Add(new Note {Text =Hello,world});
db.Notes.Add(new Note {Text =A second note});
db.Notes.Add(new Note {Text =F Sharp});
db.SaveChanges();
}

使用(var db = new MyDBContext())
{
foreach(db.Notes中的var note)
{
Console.WriteLine(Note {0} = {1},note.NoteId,note.Text);
}
}

Console.Write(按任意键...);
Console.ReadKey();
}

public class Note
{
public long NoteId {get;组; }
public string Text {get;组; }
}

public class MyDBContext:DbContext
{
//默认构造函数应该自动执行,但在这种情况下失败
public MyDBContext()
:base(MyDBContext)
{

}
public DbSet< Note>注意{get;组;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove< PluralizingTableNameConvention>();
}
}
}

如果我手动创建表程序工作正常,表格更新。如果我删除数据库,EF创建它,但不创建表,并且程序尝试读回数据时出现错误消息,表不存在。


$ b $有没有人设法获得Code First与EF6合作呢?感谢帮助/指导,因为我现在完全卡住了!!!



感谢所有。

解决方案

不幸的是, System.Data.SQLite.EF6 中的EF6提供程序实现不支持创建表。我下载了SQLite源代码来看看,但找不到任何用于创建表和迁移的东西。 EF6提供程序基本上与Linq实现相同,所有这些都是为了查询数据库而不是修改它。



我目前正在使用SQL Server和使用 SQL Server Compact& SQLite工具箱。然后可以使用 SQLiteCommand 运行脚本来模拟迁移。



更新 / p>

EF7 中,支持SQL Server compact已被删除,新的提供者对于SQLite正在由EF团队开发。提供商将使用Microsoft的托管SQLite包装器项目, Microsoft.Data.SQLite 而不是 System.Data.SQLite 项目。这也将允许在iOS,Android,Windows Phone / Mobile,Linux,Mac等上使用EF7,因为Microsoft的包装器正在开发为便携式图书馆。



仍然在测试版,但您可以从MyGet的ASP.Net开发Feed中获取nuget软件包( dev 主页发布)如果您愿意看一下。查找 EntityFramework.SQLite 包。


Using latest versions of EF6 and SQLite from NuGet. I finally got the app.config file to work after some useful posts on Stackoverflow. Now the problem is that the tables are not being created although the database is.

My app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <providers>
      <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" />
      <add name="SQLite Data Provider"
           invariant="System.Data.SQLite"
           description=".Net Framework 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>
  <connectionStrings>
    <add name="MyDBContext"
          connectionString="Data Source=|DataDirectory|MyDB.sqlite"
          providerName="System.Data.SQLite" />
  </connectionStrings>
</configuration>

My simple test program:

class Program
{
    static void Main(string[] args)
    {
        using (var db = new MyDBContext())
        {
            db.Notes.Add(new Note { Text = "Hello, world" });
            db.Notes.Add(new Note { Text = "A second note" });
            db.Notes.Add(new Note { Text = "F Sharp" });
            db.SaveChanges();
        }

        using (var db = new MyDBContext())
        {
            foreach (var note in db.Notes)
            {
                Console.WriteLine("Note {0} = {1}", note.NoteId, note.Text);
            }
        }

        Console.Write("Press any key . . . ");
        Console.ReadKey();
    }

    public class Note
    {
        public long NoteId { get; set; }
        public string Text { get; set; }
    }

    public class MyDBContext : DbContext
    {
        // default constructor should do this automatically but fails in this case
        public MyDBContext()
            : base("MyDBContext")
        {

        }
        public DbSet<Note> Notes { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

If I create the table manually the program works fine and the table is updated. If I delete the database, EF creates it but doesn't create the table and the program fails when it attempts to read back the data with an error message that the table does not exist.

Has anyone managed to get Code First working with EF6 yet? Would appreciate help/guidance on this as I'm now completely stuck!!!

Thanks all.

解决方案

Unfortunately, the EF6 provider implementation in System.Data.SQLite.EF6 doesn't support creating tables. I downloaded the SQLite source code to have a look but couldn't find anything for creating tables and for migrations. The EF6 provider is basically the same as their Linq implementation so it's all aimed at querying the database rather than modifying it.

I currently do all of my work with SQL Server and generate sql scripts for SQLite using the SQL Server Compact & SQLite Toolbox. The scripts can then be run using an SQLiteCommand to simulate migrations.

Update

In EF7 support for SQL server compact has been dropped and a new provider for SQLite is being developed by the EF team. The provider will use Microsoft's managed SQLite wrapper project, Microsoft.Data.SQLite rather than the System.Data.SQLite project. This will also allow for using EF7 on iOS, Android, Windows Phone / Mobile, Linux, Mac etc. as Microsoft's wrapper is being developed as a portable library.

It's all still in beta but you can get nuget packages from the ASP.Net development feeds at MyGet (dev, master, release) if you wish to have a look. Look for the EntityFramework.SQLite package.

这篇关于实体框架6与SQLite 3代码第一 - 不会创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆