Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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; }
}
like image 973
witoong623 Avatar asked Mar 12 '17 17:03

witoong623


1 Answers

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();
} 
like image 100
Moumit Avatar answered Oct 28 '22 13:10

Moumit