HOWTO:SQLite与EntityFramework和Code-First [英] HOWTO: SQLite with EntityFramework and Code-First

查看:168
本文介绍了HOWTO:SQLite与EntityFramework和Code-First的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用EF创建一个嵌入式SQLite数据库,但是我无法使其工作,数据库文件永远不会被创建。

I am trying to create an embedded SQLite database on the fly with the EF however, I can't get it to work, the database file is never getting created.

我有EF 4.2和最新版本的SQLite

I have EF 4.2 and latest version SQLite

这是我有什么

app.config

<?xml version="1.0"?>
<configuration>
  <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"/>
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="DataContext"
         connectionString="Data Source=test.db;Version=3;New=True;"
         providerName="System.Data.SQLite" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" />
  </startup>
</configuration>

DB初始化程序(放入一些内容)

class PageDbInitializer : DropCreateDatabaseAlways<PageDB>
{
    protected override void Seed(PageDB context)
    {
        for (int i = 0; i < 10; i++)
        {
            WebPage page = new WebPage() { Name = "Page" + (i + 1) };
            context.Pages.Add(page);
        }
        base.Seed(context);
    }
}

DbContext: p>

DbContext:

class PageDB : DbContext
{
        public DbSet<WebPage> Pages { get; set; }
}

最后在main()

Database.SetInitializer( new PageDbInitializer() );

我相信我有一些缺失的步骤,但找不到它们。

I believe I have some steps missing, but can't find them out.

推荐答案

System.Data.SQLite 仅在当前版本的EFv1兼容提供程序( 1 2 )。这意味着它没有EFv4(EFv4.2是围绕EFv4的包装)功能,包括数据库创建和删除(检查 DbProviderServices .NET 3.5和.NET 4.0在< a href =http://msdn.microsoft.com/en-us/library/system.data.common.dbproviderservices_methods%28v=VS.100%29.aspx =noreferrer> MSDN ) 。因为在使用SQLite时,您无法使用自动数据库创建代码。您必须手动创建数据库和所有表。

System.Data.SQLite is in current version only EFv1 compatible provider (1, 2). It means it doesn't have EFv4 (EFv4.2 is wrapper around EFv4) features including database creation and deletion (check DbProviderServices differences for .NET 3.5 and .NET 4.0 in MSDN). Because of that you cannot use automatic database creation with code first when working with SQLite. You must create database and all tables manually.

作为替代,您可以为SQLite使用不同的提供程序。例如 Devart的SQLite提供声明,它支持EFv4和4.1,从而创建数据库。

As an alternative you can use different provider for SQLite. For example Devart's SQLite provider claims it supports EFv4 and 4.1 and thus database creation.

这篇关于HOWTO:SQLite与EntityFramework和Code-First的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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