私が最初にSQLiteのとEntityFramework 6コードでthis questionを実装しようとしています。私はデータベースを作成することができますが、ファイルはありますが空ですが、データベースにエントリを追加しようとすると、そのようなテーブルはありません。しかし、テーブルを手動で作成しようとすると(db.Database.ExecuteSqlCommand("CREATE TABLE etc.)
)、テーブルがすでに存在することがわかります。SQLiteの
私はこれについて既にいくつかの質問を見つけましたが、絶対的なものではないパスに関するものですが、ここではそうではありません。 EF6のためのSQLiteプロバイダは、コードファースト/移行のワークフローをサポートしていないので、手動で作成する必要があります
コンテキスト
//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>
これは私の問題で変わっていません。 :/ –
SQLite Browserアプリケーションを使用して、テーブルが実際に存在するかどうか、正しいスキーマがあるかどうかを確認してください。 –
メモ帳でファイルを開くと、完全に空です(0kB)。私はかつてそれを何とか作成していましたが、私はもうこれを再現することができません(私はそれを確認できません)。それからそれは8kBを持っていて、たくさんのNUL文字で満たされました。だから私はテーブルの作成と保存の問題だと思うが、私は手動で作成することはできません質問に記載されているように。 –