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