【问题标题】:One To Many relationship data seeding in .NET Core.NET Core 中的一对多关系数据播种
【发布时间】:2020-02-24 06:01:52
【问题描述】:

我在使用 OnModelCreating 方法将数据播种到我的数据库时遇到问题。

我想在我的数据库中添加 Countries、States 和 Cities,但是当他们有 States 时我不知道该怎么做em>一对多关系。

型号:

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

上下文:

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

上面的代码抛出:

无法将类型“State”标记为拥有,因为已存在同名的非拥有实体类型。

当我尝试AddMigration时。

我的目标是为数据库播种一个国家及其相关的州和城市。

【问题讨论】:

    标签: c# sql-server entity-framework .net-core


    【解决方案1】:

    Getting Started with EF Core 和 Relationships 页面有一个很好的一对多映射示例。

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

    在您的情况下,City 需要有 StateId,State 需要有 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 的 Data Seeding 包含一个为一对多关系模型播种数据的示例。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 2021-11-05
      • 2021-06-30
      • 2019-01-05
      • 2019-11-17
      • 2013-06-21
      • 1970-01-01
      相关资源
      最近更新 更多