use*_*887 40 .net sqlite system.data.sqlite ef-code-first entity-framework-6
使用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>
Run Code Online (Sandbox Code Playgroud)
我的简单测试程序:
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>();
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我手动创建表,程序工作正常,表更新.如果我删除数据库,EF会创建它,但不会创建表,并且程序在尝试使用表不存在的错误消息读回数据时会失败.
有没有人设法让Code First与EF6合作?非常感谢帮助/指导,因为我现在完全陷入困境!
谢谢大家.
msa*_*lin 52
我从Fried的代码开始,创建了一个允许您使用CodeFirst的项目.该项目在GitHub或NuGet Package上提供开源.
如果您遇到任何问题或错过功能,请随时在GitHub上打开一个新问题.当然PR很受欢迎.
编辑(2016年4月26日):
与此同时,我在这个项目中做了很多.
支持以下(默认EF)功能:
SQLite还有一些独有的功能,默认情况下不支持:
有两种方法可以使用此库的功能.
使用DbInitializers:
通过使用以下两个类之一获得更多控制:
kjb*_*tel 40
不幸的是,EF6提供程序实现System.Data.SQLite.EF6不支持创建表.我下载了SQLite源代码以查看但无法找到任何用于创建表和迁移的内容.EF6提供程序与它们的Linq实现基本相同,因此它们都旨在查询数据库而不是修改它.
我目前正在使用SQL Server完成所有工作,并使用SQL Server Compact和SQLite工具箱为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包裹.
fri*_*ied 14
我决定编写自己的基础数据库初始化程序来解决这个问题.
你可以在这里查看:https: //gist.github.com/flaub/1968486e1b3f2b9fddaf
它支持:
| 归档时间: |
|
| 查看次数: |
53216 次 |
| 最近记录: |