I'm having trouble seeding data to my database at OnModelCreating method.
I want to seed my database with Countries, States and Cities but I don't know how to do it when they have one-to-many relationships.
Models:
public class City
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual State State { get; set; }
}
public class State
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<City> Cities { get; set; }
public virtual Country Country { get; set; }
}
public class Country
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<State> States { get; set; }
}
Context:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// ....
modelBuilder.Entity<State>()
.HasMany(p => p.Cities)
.WithOne(c => c.State)
.IsRequired();
modelBuilder.Entity<Country>()
.HasMany(s => s.States)
.WithOne(s => s.Country)
.IsRequired();
var BsAs = new State
{
Id = 1,
Name = "Buenos Aires",
};
var Argentina = new Country
{
Id = 1,
Name = "Argentina",
States = new[] { BsAs }
};
modelBuilder.Entity<Country>(s => {
s.HasData(Argentina);
s.OwnsMany(e => e.States).HasData(BsAs);
});
}
The code above throws:
The type 'State' cannot be marked as owned because a non-owned entity type with the same name already exists.
when I try to AddMigration.
My goal is to seed the database with one country and its respecting states and cities.