使用Entity Framework Core和SQLite遷移數據庫時,表已存在異常

entity-framework-core sqlite uwp

我在UWP中使用EF Core和SQLite。我嘗試通過調用DbContext.Database.Migrate()進行遷移,但我總是得到Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 1: 'table "Tags" already exists'.'

我確定該表不存在,因為我檢查了bin / debug文件夾,沒有數據庫文件。即使我在錯誤的文件夾中檢查,也不應該有問題嗎?
我已經多次刪除了Migrations文件夾,但我認為這不是導致此異常的原因。

這是DbContext代碼。

public class AppDbContext : DbContext
{
    public DbSet<Word> Words { get; set; }
    public DbSet<WordMeaning> WordMeanings { get; set; }
    public DbSet<Tag> Tags { get; set; }
    public DbSet<WordTag> WordTags { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Data Source=Vocabulary.db");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // PK declaration
        modelBuilder.Entity<Word>()
            .HasKey(w => w.Text);
        modelBuilder.Entity<WordMeaning>()
            .HasKey(wm => new { wm.WordText, wm.WordClass });
        modelBuilder.Entity<Tag>()
            .HasKey(t => t.Name);
        modelBuilder.Entity<WordTag>()
            .HasKey(wt => new { wt.WordText, wt.TagName });

        // relation declaration
        modelBuilder.Entity<Word>()
            .HasMany(w => w.WordMeanings)
            .WithOne(wm => wm.Word)
            .HasForeignKey(wm => wm.WordText)
            .IsRequired()
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<WordTag>()
            .HasOne(wt => wt.Tag)
            .WithMany(t => t.WordTag)
            .HasForeignKey(wt => wt.TagName);
        modelBuilder.Entity<WordTag>()
            .HasOne(wt => wt.Word)
            .WithMany(w => w.WordTag)
            .HasForeignKey(wt => wt.WordText);
    }
}

和所有模型代碼。

public class Tag
{
    public string Name { get; set; }
    public string Description { get; set; }
    public List<WordTag> WordTag { get; set; }
}
public class Word
{
    public string Text { get; set; }
    public WordClass WordClasses { get; set; }
    public DateTime AddedDate { get; set; }
    public List<WordMeaning> WordMeanings { get; set; }
    public List<WordTag> WordTag { get; set; }
}
public class WordMeaning
{
    public string WordText { get; set; }
    public string Definition { get; set; }
    public string Example { get; set; }
    public WordClass WordClass { get; set; }
    public Word Word { get; set; }
}
public class WordTag
{
    public Word Word { get; set; }
    public Tag Tag { get; set; }
    public string WordText { get; set; }
    public string TagName { get; set; }
}

一般承認的答案

@Gert Arnold說,默認情況下,你應該在LocalFolder上創建你的SQLite數據庫文件(Vocabulary.db)。您應該能夠找到已在C:\Users\{username}\AppData\Local\Packages\{your app package name}\LocalState)上創建Tag表的數據庫。您可以通過Package.appxmanifest->Packing->Package name在項目中找到Package.appxmanifest->Packing->Package name 。有關uwp app上文件訪問的更多詳細信息,請參閱文件,文件夾和庫

有關uwp的實體框架的更多細節請參考UWP - 新數據庫



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow