C# EF 6.1尝试建立递归关系时出错-自引用表和FluentAPI

C# EF 6.1尝试建立递归关系时出错-自引用表和FluentAPI,c#,entity-framework,foreign-key-relationship,ef-fluent-api,C#,Entity Framework,Foreign Key Relationship,Ef Fluent Api,我在EntityFramework6.1中创建递归关系时遇到了很大的问题 我需要一些不同类型的类别,但已经为所有类别创建了一个“基本”抽象类。我使用的是每种类型表的分层策略。类别可以有父类别,也可以没有父类别。(如果不是,则为顶级类别) 在下面的代码中,我将展示如何创建抽象类别类以及ParentCategory和ChildCategories导航属性。我已经将ParentCategoryId设置为可空,因为顶级类别不需要它。我已经看到了一些帖子,这些帖子正是我在这里想要实现的,尽管我认为我已经找

我在EntityFramework6.1中创建递归关系时遇到了很大的问题

我需要一些不同类型的类别,但已经为所有类别创建了一个“基本”抽象类。我使用的是每种类型表的分层策略。类别可以有父类别,也可以没有父类别。(如果不是,则为顶级类别)

在下面的代码中,我将展示如何创建抽象类别类以及ParentCategory和ChildCategories导航属性。我已经将ParentCategoryId设置为可空,因为顶级类别不需要它。我已经看到了一些帖子,这些帖子正是我在这里想要实现的,尽管我认为我已经找到了所有答案,但我仍然得到了以下错误:

Category\u ParentCategory::多重性与关系“Category\u ParentCategory”中角色“Category\u ParentCategory\u Target”中的引用约束冲突。由于从属角色中的所有属性都不可为空,因此主体角色的多重性必须为“1”。

我想知道它是否与Category作为一个抽象类和我正在使用的添加的继承模型有关,因为我在其他任何关于这种递归关系的文章中都没有看到这种确切用法。感谢您的帮助

public abstract class Category : ICategory
{
    protected Category()
    {           
        ChildCategories = new HashSet<Category>();
    }

    public int CategoryId { get; private set; }
    public int? ParentCategoryId { get; set; }
    public virtual Category ParentCategory { get; set; }
    public virtual ICollection<Category> ChildCategories { get; set; }
}



public class WLCategory : Category, IWLCategory
{
    public WLCategory() : base()
    {
        DynamicFields = new HashSet<DynamicFieldDef>();
    }
    public virtual ICollection<DynamicFieldDef> DynamicFields { get; set; } 
}
公共抽象类类别:ICategory
{
受保护类别()
{           
ChildCategories=新HashSet();
}
public int CategoryId{get;private set;}
public int?ParentCategoryId{get;set;}
公共虚拟类别ParentCategory{get;set;}
公共虚拟ICollection子类别{get;set;}
}
公共类WLCategory:类别,IWLCategory
{
public WLCategory():base()
{
DynamicFields=新的HashSet();
}
公共虚拟ICollection动态字段{get;set;}
}
使用FluentAPI,我将数据库创建配置为:

class CategoriesConfig : EntityTypeConfiguration<Category>
{
    public CategoriesConfig()
    {          
        HasOptional(p => p.ParentCategory).WithMany(p => p.ChildCategories)
            .HasForeignKey(p => p.ParentCategoryId);

        ToTable("Categories");
    }
}



class WLCategoriesConfig : EntityTypeConfiguration<WLCategory>
{
    public WLCategoriesConfig()
    {
        HasKey(p => p.CategoryId);
        ToTable("WLCategories");
    }
}
class CategoriesConfig:EntityTypeConfiguration
{
公共分类配置()
{          
has可选(p=>p.ParentCategory)。带有多个(p=>p.ChildCategories)
.HasForeignKey(p=>p.ParentCategoryId);
ToTable(“类别”);
}
}
类别WLCategoriesConfig:EntityTypeConfiguration
{
公共WLCategoriesConfig()
{
HasKey(p=>p.CategoryId);
ToTable(“WLCategories”);
}
}

好的,找到问题了!在OnModelCreating方法中,我设置了一些全局配置,其中之一是:

 modelBuilder.Properties().Configure(p => p.IsRequired());
我想在默认情况下将所有属性设置为不可为null,除非我在一个类一个类的基础上显式地重写属性。但是,我不认为此全局设置会应用于以下数据类型:

int? (nullable int)
显然,我错了。(不确定为什么EF会尝试使可为null的int在DB中不可为null,即使开发人员在全局范围内设置为必需的。int?的固有含义是“可为null”。)我必须在HasOptional语句之前向我的CategoriesConfig类显式添加以下代码行:

 Property(p => p.ParentCategoryId).IsOptional();
现在,一切都好了。:-)