【问题标题】:table already exists exception when migrate DB using Entity Framework Core and SQLite使用 Entity Framework Core 和 SQLite 迁移数据库时,表已存在异常
【发布时间】:2017-08-02 17:21:02
【问题描述】:

我在 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; }
}

【问题讨论】:

  • 你检查你的用户文件夹了吗?喜欢 W10 上的C:\user\username

标签: sqlite uwp entity-framework-core


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    更多关于实体框架与uwp的细节请参考UWP - New Database

    【讨论】:

    • 谢谢,现在我可以删除 *.db 并且 Migrate() 不再抛出异常了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 2017-10-27
    • 2022-12-09
    • 2017-04-26
    • 2017-05-16
    • 1970-01-01
    • 2016-08-21
    相关资源
    最近更新 更多