Entity framework 代码优先外键配置

Entity framework 代码优先外键配置,entity-framework,Entity Framework,我很难维持父类与其子类之间的多重关系。谁能告诉我为什么我可以在父对象中创建两个子对象引用,但不能创建第三个子对象引用?下面的代码仅在注释掉第三个引用时有效 public class Parent { public int Id { get; set; } public string Name { get; set; } public int Child1Id { get; set; } public Child Child1 { get; set; } p

我很难维持父类与其子类之间的多重关系。谁能告诉我为什么我可以在父对象中创建两个子对象引用,但不能创建第三个子对象引用?下面的代码仅在注释掉第三个引用时有效

public class Parent
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Child1Id { get; set; }
    public Child Child1 { get; set; }
    public int Child2Id { get; set; }
    public Child Child2 { get; set; }
    //public int Child3Id { get; set; }
    public Child Child3 { get; set; }
    public ICollection<Child> Children { get; set; }
}
public class Child
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ParentId { get; set; }
    public Parent Parent { get; set; }
}
public class CFContext : DbContext
{
    public DbSet<Parent> Parents { get; set; }
    public DbSet<Child> Children { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Child>()
            .HasRequired(c => c.Parent)
            .WithRequiredPrincipal(p => p.Child1)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Child>()
         .HasRequired(c => c.Parent)
         .WithRequiredPrincipal(p => p.Child2)
         .WillCascadeOnDelete(false);

        //modelBuilder.Entity<Child>()
        // .HasRequired(c => c.Parent)
        // .WithRequiredPrincipal(p => p.Child3)
        // .WillCascadeOnDelete(false);
    }
}
公共类父类
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int Child1Id{get;set;}
公共子Child1{get;set;}
public int Child2Id{get;set;}
公共子Child2{get;set;}
//public int Child3Id{get;set;}
公共子Child3{get;set;}
公共ICollection子项{get;set;}
}
公营儿童
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int ParentId{get;set;}
公共父级{get;set;}
}
公共类CFContext:DbContext
{
公共数据库集父项{get;set;}
公共DbSet子项{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasRequired(c=>c.Parent)
.WithRequiredPrincipal(p=>p.Child1)
.WillCascadeOnDelete(假);
modelBuilder.Entity()
.HasRequired(c=>c.Parent)
.WithRequiredPrincipal(p=>p.Child2)
.WillCascadeOnDelete(假);
//modelBuilder.Entity()
//.HasRequired(c=>c.Parent)
//.WithRequiredPrincipal(p=>p.Child3)
//.WillCascadeOnDelete(假);
}
}

看起来您正在尝试从父实体到子实体建立一对多关系。在这种情况下,代码应该如下所示:

public class Parent
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Child> Children { get; set; }
}
public class Child
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ParentId { get; set; }
    public Parent Parent { get; set; }
}
公共类父类
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection子项{get;set;}
}
公营儿童
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int ParentId{get;set;}
公共父级{get;set;}
}
只要遵循有关导航属性和外键命名的默认约定,就不必在Fluent API中指定关系。您必须使用Fluent API和/或属性来配置您使用非约定名称的关系,例如重命名ParentId。其他一些东西需要您使用at[ForeignKey(“Parent”)]属性对其进行标记


使用Fluent API最常见的用例是禁用级联删除(使用属性无法做到这一点)

父对象需要维护对子对象的三个引用。代码首先似乎允许2个循环引用,但第三个循环引用受阻。在我看来,至少3个引用的要求不应该在数据库层强制执行。这是业务逻辑层应该注意的事情。我喜欢在代码中不首先使用属性,而是使用模型生成器。这意味着您的poco类不需要任何EF引用(因此可以更轻松地通过应用程序传输)。我写了一篇关于nav属性和modelbuilder配置的文章,这里可能值得一读。一般来说,如果您发现自己命名实体Entity1、Entity2、Entity3等,那么您在设计级别上犯了一些错误,应该重新考虑您的方法