我正在尝试使用SQLite和EntityFramework 6的代码优先方式来实现这个问题。我可以创建数据库并且有一个文件(虽然为空),但是当我尝试向数据库添加条目时,它会说没有这样的表。然而,当我尝试手动创建表格(
我已经找到了一些关于这个问题的解答,但它们都是关于路径不是绝对路径的问题,但这在这里显然不是问题。
db.Database.ExecuteSqlCommand("CREATE TABLE etc.)
)时,它告诉我该表已经存在。我已经找到了一些关于这个问题的解答,但它们都是关于路径不是绝对路径的问题,但这在这里显然不是问题。
上下文
//Context:
class MediaContext : DbContext
{
public DbSet<MediaModel> Media { get; set; }
public MediaContext(string filename): base(new SQLiteConnection() {
ConnectionString =
new SQLiteConnectionStringBuilder()
{ DataSource = filename, ForeignKeys = false }
.ConnectionString
}, true)
{
}
}
DAL
//In the DAL:
public DataAccessLayer(string path)
{
_path = Path.GetFullPath(path); //<--The path already comes full in, I'm just paranoid at this point.
_connectionPath = _path + @"\MyDatabase.sqlite";
using (var db = new MediaContext(_connectionPath))
{
db.Database.CreateIfNotExists();
db.Database.Initialize(false);
db.SaveChanges();
}
public void Generate()
{
using (var db = new MediaContext(_connectionPath))
{
db.Media.Add(new MediaModel("test"));
db.SaveChanges(); //<--SQLiteException: SQL logic error or missing database: no such table: MediaModels
}
}
App.Config
//App.config:
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<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" />
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
</configuration>