.net - 将 Entity Framework 4.1 与 SQLITE 结合使用

标签 .net entity-framework sqlite entity-framework-4.1

有可能吗?我有一个现有的数据库,并创建了实体类:

public class MyContainer : DbContext
{
    public DbSet<Item> Items { get; set; }
    // more tables..
}

当我使用此连接字符串时,它会失败,因为它没有任何元数据文件:
  <connectionStrings>    
  <add name="MyContainer" 
         connectionString="metadata=.\items.csdl|.\items.ssdl|.\items.msl;provider=System.Data.SQLite;provider connection string=&quot;data source=.\mindstore.sqlite.s3db&quot;" 
         providerName="System.Data.EntityClient"/>
  </connectionStrings>

但是当我从配置中省略元数据时,它会提示您不允许从配置中省略元数据。

那么如何在没有任何 xml 配置文件的情况下将 SQLITE 与 EF 4.1 一起使用并且只根据约定进行映射呢?

最佳答案

如果您想在没有 EDMX 的情况下首先使用 EF 代码,则不能使用 EntityClient .对 SQLite 使用常见的 ADO.NET 连接字符串:

<connectionStrings>    
    <add name="MyContainer" providerName="System.Data.SQLite"
         connectionString="data source=.\mindstore.sqlite.s3db" />
</connectionStrings>

编辑:

要关闭数据库创建和数据库检查,请尝试删除默认元数据约定并将数据库初始值设定项设置为 null。

数据库初始化程序:
// call this only once in your application bootstrap
Database.SetInitializer<YourContext>(null);

删除约定:
// Place this to your derived context
protected override void OnModelCreating(ModelBuilder modelBuilder) 
{
    base.OnModelCreationg(modelBuilder);

    modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}

关于.net - 将 Entity Framework 4.1 与 SQLITE 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6101812/

相关文章:

sqlite - 输出树的打开和关闭元素

.net - 可选参数 : "empty" vs "not provided"

c# - 正则表达式向前看和向后看最多一位数

c# - 在 C# 中定义接口(interface)时, 'in' 和 '@' 关键字有什么作用?

c# - 模拟 DbContext - 无法插入新对象

c# - 没有模型属性的 Entity Framework 映射结构/复杂类型

c# - 如何遍历对象的成员变量?

asp.net-mvc - ApplicationUser - 我如何使用它?我需要在 using 子句中添加什么?

ios - 在核心数据中存储上下文属性的最佳方式?

java - 将值插入 android sqlite 数据库的最快方法