加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

.net – Entity Framework 6 with SQLite 3 Code First – 不会

发布时间:2020-12-12 19:16:36 所属栏目:百科 来源:网络整理
导读:使用NuGet的最新版本的EF6和SQLite。我终于得到了app.config文件工作后一些有用的帖子Stackoverflow。现在的问题是,虽然数据库是不创建表。 我的app.config: ?xml version="1.0" encoding="utf-8"?configuration configSections !-- For more information
使用NuGet的最新版本的EF6和SQLite。我终于得到了app.config文件工作后一些有用的帖子Stackoverflow。现在的问题是,虽然数据库是不创建表。

我的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>

我的简单测试程序:

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>();
        }
    }
}

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

有人设法让Code First使用EF6吗?将欣赏帮助/指导,因为我现在完全卡住!

谢谢大家。

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

我目前正在使用SQL Server的所有工作,并使用SQL Server Compact & SQLite Toolbox为SQLite生成sql脚本。然后可以使用SQLiteCommand运行脚本来模拟迁移。

更新

在EF7年,对SQL服务器压缩的支持被放弃,并且EF团队正在开发SQLite的新提供程序。提供程序将使用Microsoft的受管SQLite包装程序项目,Microsoft.Data.SQLite而不是System.Data.SQLite项目。这也将允许在iOS,Android,Windows Phone / Mobile,Linux,Mac等上使用EF7,因为Microsoft的包装程序正在开发为便携式库。

它仍然处于测试阶段,但是如果你想看看,你可以从MyGet(dev,master,release)的ASP.Net开发源获取nuget包。查找EntityFramework.SQLite包。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读