使用Entity Framework Core和SQLite迁移数据库时,表已存在异常

时间:2017-03-12 17:52:25

标签: sqlite uwp entity-framework-core

我在UWP中使用EF Core和SQLite。我尝试通过调用DbContext.Database.Migrate()进行迁移,但我总是得到Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 1: 'table "Tags" already exists'.'

我确定表格不存在,因为我检查了bin / debug文件夹,没有数据库文件。即使我检查错误的文件夹,也不应该有问题吗? 我已经多次删除了迁移文件夹,但我不认为这是此异常的原因。

这是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; }
}

2 个答案:

答案 0 :(得分:1)

@Gert Arnold说,默认情况下,应在LocalFolder上创建您的SQLite数据库文件(Vocabulary.db)。您应该能够找到Tag上已创建C:\Users\{username}\AppData\Local\Packages\{your app package name}\LocalState)表的数据库。您可以在项目中通过Package.appxmanifest->Packing->Package name找到的包名称。有关uwp应用程序上文件访问的更多详细信息,请参阅Files, folders, and libraries

有关使用uwp的实体框架的更多详细信息,请参阅UWP - New Database

答案 1 :(得分:0)

请勿同时使用EnsureCreatedMigrate,仅迁移足以创建和迁移数据库

using (var db = new DBContext())
{
    //db.Database.EnsureCreated(); Don't use
    db.Database.Migrate();
}