Hax
Hax

Reputation: 154

EnsureCreated method cannot create new tables in db

I'm using context.Database.EnsureCreated(); to map and create automatically tables considering their entities on Mac OS. But, it doesn't create new tables for new entities newly created.

When I run the following codes, I see 3 tables in the db. But when I add Battle.cs and uncomment -

public DbSet<Battle> Battles { get; set; }

in DbContext, the context.Database.EnsureCreated(); call doesn't create the Battle table in database.

Models -

public class Samurai
{
    public Samurai()
    {
        Quotes = new List<Quote>();
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Quote> Quotes { get; set; }
    public Clan Clan { get; set; }
}

public class Clan
{
    public int Id { get; set; }
    public string ClanName { get; set; }
}

public class Quote
{
    public int Id { get; set; }
    public string Text { get; set; }
    public Samurai Samurai { get; set; }
    public int SamuraiId { get; set; }
}

DbContext -

public class SamuraiContext : DbContext
{
    public DbSet<Samurai> Samurais { get; set; }
    public DbSet<Quote>   Quotes   { get; set; }
    public DbSet<Clan>   Clans   { get; set; }
    // public DbSet<Battle> Battles { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseMySQL(@"Server=localhost;port=3306;Database=exercise;Uid=root;Pwd=password;allowPublicKeyRetrieval=true;");
        base.OnConfiguring(optionsBuilder);
    }
}

The Battle model -

public class Battle
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime StartDate { get; set;}
    public DateTime EndDate { get; set; }
}

Program -

class Program
{
    private static SamuraiContext context = new SamuraiContext();

    static void Main(string[] args)
    {
        context.Database.EnsureCreated();
    }
}

Upvotes: 2

Views: 3635

Answers (1)

Leaky
Leaky

Reputation: 3636

As Jeremy commented, EnsureCreated() simply doesn't do what you want to do. From the documentation page:

Ensures that the database for the context exists. If it exists, no action is taken. If it does not exist then the database and all its schema are created. If the database exists, then no effort is made to ensure it is compatible with the model for this context.

EnsureCreated() is great if for example you're integration testing on an in-memory SQLite database.

Normally you want to generate a schema for the current model by running dotnet ef migrations add <MigrationName>, and then if you want to programmatically ensure that the database reflects the current schema, call database.Migrate(). Of course you can also call dotnet ef database update from the cli.

That said, if you're working with a throwaway database of some sorts, and you don't want to bother at all with generating schema or migrations, then ...

database.EnsureDeleted();
database.EnsureCreated();

... should work fine, because these Ensure* methods don't rely on migrations.

(I just tested it by adding an extra entity to a DbContext, then calling EnsureDeleted() followed by EnsureCreated() without a prior migration, and it indeed recreated the database, with a table for the new entity as well.)

If you have additional questions, feel free to comment, and I'll address them. :)

Upvotes: 8

Related Questions