gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:55:36 28 4
gpt4 key购买 nike

我在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)。您应该能够找到已经在Tag上创建的C:\Users\{username}\AppData\Local\Packages\{your app package name}\LocalState)表的数据库。您可以在项目上通过Package.appxmanifest->Packing->Package name找到包名称。有关uwp应用程序上文件访问的更多详细信息,请引用Files, folders, and libraries

有关使用uwp的 Entity Framework 的更多详细信息,请引用UWP - New Database

关于sqlite - 使用Entity Framework Core和SQLite迁移数据库时,表已存在异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42750991/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com