table already exists exception when migrate DB using Entity Framework Core and SQLite

entity-framework-core sqlite uwp

Question

I'm using EF Core and SQLite in UWP. I tried to migrate by calling DbContext.Database.Migrate() but I always get Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 1: 'table "Tags" already exists'.'.

I'm sure that the table doesn't exist because I checked on bin/debug folder, there is no database file. Even I check in wrong folder, there shouldn't have a problem doesn't it?
I've deleted Migrations folder many times but I don't think this is the cause of this exception.

This is DbContext code.

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);
    }
}

and all of models code.

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; }
}
1
3
3/12/2017 6:52:36 PM

Accepted Answer

@Gert Arnold said, Your SQLite database file (Vocabulary.db) should be created on the LocalFolder by default. You should be able to find the database with Tag table is already created on C:\Users\{username}\AppData\Local\Packages\{your app package name}\LocalState). The package name you can find by Package.appxmanifest->Packing->Package name on your project. More details about the file access on uwp app please reference Files, folders, and libraries.

And more details about entity framework with uwp please reference UWP - New Database.

1
3/13/2017 9:08:58 AM

Popular Answer

Do not use both EnsureCreated and Migrate, Only Migrate enough to create and migrate the DB

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow