C# 在实体框架核心中播种关系数据

C# 在实体框架核心中播种关系数据,c#,entity-framework-core,C#,Entity Framework Core,我在这上面找到了好几篇这样的帖子,但无法确定我在这里做错了什么 我正试着在两张桌子上各放一个项目。第二项包含对第一项的查找。我可以毫无疑问地为父级设置种子,但无法找出正确的语法来为子级设置种子 下面是我用来为父级设置种子的代码 modelBuilder.Entity<Group>().HasData(new Group { Id = 1, Name = "Any", Is

我在这上面找到了好几篇这样的帖子,但无法确定我在这里做错了什么

我正试着在两张桌子上各放一个项目。第二项包含对第一项的查找。我可以毫无疑问地为父级设置种子,但无法找出正确的语法来为子级设置种子

下面是我用来为父级设置种子的代码

   modelBuilder.Entity<Group>().HasData(new Group
        {
            Id = 1,
            Name = "Any",
            IsDeleted = false
        });
它显然插入了一个“0”作为GroupId,尽管它在seed中被硬编码为“1”;我不明白为什么会这样。下面是表示这两个表的两个实体的代码:

public class Group
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsDeleted { get; set; }
    public ICollection<ApplicationUser> Users { get; set; }

    public Group()
    {
    }

    public Group(string name)
    {
        Name = name;
    }
}

public class ShiftTypeDb
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsDeleted { get; set; }
    public int GroupId { get; set; }
    public Group Group { get; set; }

    public ShiftTypeDb()
    {
    }
}
公共类组
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共布尔被删除{get;set;}
公共ICollection用户{get;set;}
公共组()
{
}
公共组(字符串名称)
{
名称=名称;
}
}
公共类ShiftTypeDb
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共布尔被删除{get;set;}
public int GroupId{get;set;}
公共组组{get;set;}
公共移位类型数据库()
{
}
}

此代码在Visual Studio中经过测试,运行正常。修复所有错误后,您必须删除现有数据库和迁移,并使用addmigrationinitialcreate选项创建新数据库

你有一对一的关系,而不是一对多的关系。要修复此错误,请将ShiftTypeDb集合添加到Group类:

    public class Group
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsDeleted { get; set; }
    public virtual ICollection<ApplicationUser> Users { get; set; }

    public virtual ICollection<ShiftTypeDb> ShifTypeDbs { get; set; }

   ...other properties
}
公共类组
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共布尔被删除{get;set;}
公共虚拟ICollection用户{get;set;}
公共虚拟ICollection ShifTypeDbs{get;set;}
……其他财产
}
AN使用ShiftTypeDb而不是匿名类型。仅当类具有阴影属性时,才会使用匿名类型。并确保该组在TypeDB之前

modelBuilder.Entity<ShiftTypeDb>(
                entity =>
                {
                    entity.HasOne(d => d.Group)
                        .WithMany(p => p.ShiftTypeDbs)
                        .HasForeignKey("GroupId");
                });
 

modelBuilder.Entity<ShiftTypeDb>().HasData(new ShiftTypeDb 
        {
            Id = 1,
            Name = "Any",
            GroupId = 1,
            IsDeleted = false
        })
modelBuilder.Entity(
实体=>
{
entity.HasOne(d=>d.Group)
.WithMany(p=>p.ShiftTypeDbs)
.HasForeignKey(“GroupId”);
});
modelBuilder.Entity().HasData(新的ShiftTypeDb
{
Id=1,
Name=“Any”,
GroupId=1,
IsDeleted=false
})

Hi Sergey,我已经完成了上述操作(将ICollection ShiftTypes添加到组实体,使用FluentAPI代码来建立关系,并将annonymous对象更改为ShiftTypeDb的对象初始化器),但我收到了相同的错误消息。您必须重新迁移数据库Hanks Sergey,事实证明,问题是我必须删除以前的迁移才能使新的更改生效。我最终没有将ICollection添加到组集合中就可以让它工作。很抱歉,您只是没有正确测试它。这是一只虫子。如果不添加ICollection,您将无法添加另一个ShiftTypeDb记录。
    public class Group
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsDeleted { get; set; }
    public virtual ICollection<ApplicationUser> Users { get; set; }

    public virtual ICollection<ShiftTypeDb> ShifTypeDbs { get; set; }

   ...other properties
}
modelBuilder.Entity<ShiftTypeDb>(
                entity =>
                {
                    entity.HasOne(d => d.Group)
                        .WithMany(p => p.ShiftTypeDbs)
                        .HasForeignKey("GroupId");
                });
 

modelBuilder.Entity<ShiftTypeDb>().HasData(new ShiftTypeDb 
        {
            Id = 1,
            Name = "Any",
            GroupId = 1,
            IsDeleted = false
        })