Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 实体框架6和SQLite 3代码优先-赢得';不要创建表_.net_Sqlite_Ef Code First_Entity Framework 6_System.data.sqlite - Fatal编程技术网

.net 实体框架6和SQLite 3代码优先-赢得';不要创建表

.net 实体框架6和SQLite 3代码优先-赢得';不要创建表,.net,sqlite,ef-code-first,entity-framework-6,system.data.sqlite,.net,Sqlite,Ef Code First,Entity Framework 6,System.data.sqlite,使用来自NuGet的最新版本的EF6和SQLite。在Stackoverflow上发表了一些有用的帖子之后,我终于让app.config文件开始工作了。现在的问题是,尽管数据库正在创建中,但没有创建表 我的app.config: <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity

使用来自NuGet的最新版本的EF6和SQLite。在Stackoverflow上发表了一些有用的帖子之后,我终于让app.config文件开始工作了。现在的问题是,尽管数据库正在创建中,但没有创建表

我的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>();
        }
    }
}
类程序
{
静态void Main(字符串[]参数)
{
使用(var db=new MyDBContext())
{
添加(新注释{Text=“Hello,world”});
添加(新注释{Text=“第二个注释”});
db.Notes.Add(新注释{Text=“F Sharp”});
db.SaveChanges();
}
使用(var db=new MyDBContext())
{
foreach(数据库注释中的变量注释)
{
WriteLine(“Note{0}={1}”,Note.NoteId,Note.Text);
}
}
控制台。写入(“按任意键…”);
Console.ReadKey();
}
公开课堂讲稿
{
公共长NoteId{get;set;}
公共字符串文本{get;set;}
}
公共类MyDBContext:DbContext
{
//默认构造函数应该自动执行此操作,但在这种情况下失败
公共MyDBContext()
:base(“MyDBContext”)
{
}
公共DbSet注释{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
}
}
}
如果我手动创建表,程序工作正常,表也会更新。如果我删除了数据库,EF会创建它,但不会创建表,并且当程序试图读回数据时会失败,并显示一条错误消息,说明表不存在

有没有人能先用EF6编写代码?非常感谢您的帮助/指导,因为我现在完全被卡住了


谢谢大家。

不幸的是,
System.Data.SQLite.EF6
中的EF6提供程序实现不支持创建表。我下载了SQLite源代码进行查看,但找不到任何用于创建表和迁移的内容。EF6提供程序基本上与其Linq实现相同,因此其目的都是查询数据库,而不是修改数据库

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

更新

为了支持SQLServerCompact,EF团队已经放弃,并且正在开发一个新的SQLite提供程序。提供商将使用Microsoft的托管SQLite包装项目,而不是
System.Data.SQLite
项目。这也将允许在iOS、Android、Windows Phone/Mobile、Linux、Mac等平台上使用EF7,因为微软的包装器正在开发为便携式库


它仍然是beta版,但是如果您想看一看的话,可以从MyGet(,)的ASP.Net开发源获取nuget软件包。查找
EntityFramework.SQLite
包。

您也一样。我使用了Compact4.0而不是Sqlite

作为参考:


我决定编写自己的基本数据库初始值设定项来解决这个问题

您可以在这里查看:

它支持:

  • 多对多关系
  • 先编写数据注释,如:
    • [关键]
    • [必需]
    • [索引]
    • [外键]

我从fried的代码开始,创建了一个允许您使用CodeFirst的项目。该项目可在或作为开源项目提供

如果遇到任何问题或错过某个功能,请随时打开。当然,PRs非常受欢迎

编辑(2016年4月26日):

与此同时,我在这个项目中做了很多

支持以下(默认EF)功能:

  • 类中的表(支持的注释:表)
  • 属性中的列(支持的注释:列、键、MaxLength、必需、未映射、DatabaseGenerated、索引)
  • PrimaryKey约束(支持键注释、键组合)
  • ForeignKey约束(1-n关系,支持“删除时级联”)
  • 非空约束
  • 自动递增(int主密钥将自动递增)
  • 索引(使用Index属性装饰列。默认情况下,会自动为外键创建索引。要防止出现这种情况,可以删除Convertion ForeignKeyIndexConvention)
还有一些SQLite独有的功能,默认情况下不支持这些功能:

  • 唯一约束(使用UniqueAttribute装饰列,这是此库的一部分)
  • Collate约束(使用Collate属性装饰列,该属性是此库的一部分)
有两种方法可以使用此库的功能

  • 使用DBInitializer:
    • SqliteCreateDatabaseIfNotExists
    • SqliteDropCreateDatabaseAlways
    • SqliteDropCreateDatabaseWhenModelChanges
  • 通过使用以下两个类之一获得更多控制:
    • SqliteSqlGenerator(基于模型创建SQL)
    • SqliteDatabaseCreator(基于数据库和DbModel创建新的SQLite数据库)
    编辑(2020年5月30日和2021年3月20日): 由于EF6现在支持.NETCore3及更高版本,我调整了库以支持.NET标准2.1。支持以下.NET framework版本:

    • .NET 4.0(使用net40)
    • .NET 4.5-4.8(使用net45)
    • .NET核心3.0-3.1(使用netstandard2.1)
    • .NET5(使用netstandard2.1)

    Updae 3月24日:仍然没有进展,只是下载了lat