victor.ja
victor.ja

Reputation: 888

One To Many relationship data seeding in .NET Core

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.

Upvotes: 2

Views: 2683

Answers (1)

tmaj
tmaj

Reputation: 34987

Getting Started with EF Core and Relationships pages have a good example of one-to-many mapping.

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

In your case a City needs to have StateId and a State needs to have CountryId.

public class City
{
    public int Id { get; set; }
    (...)
    public int StateId { get; set; }
    public State State { get; set; }
}

public class State
{
   public int Id { get; set; }
   (...)
   public int CountryId { get; set; }
   public Country Country { get; set; }
}

public class Country
{
    public int Id { get; set; }
}

EF Core's Data Seeding contains an example of seeding data for one-to-many relationship model.

modelBuilder.Entity<Blog>().HasData(new Blog {BlogId = 1, Url = "http://sample.com"});

modelBuilder.Entity<Post>().HasData(
    new Post() { BlogId = 1, PostId = 1, Title = "First post", Content = "Test 1" });

Upvotes: 4

Related Questions