Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF代码中自引用实体的映射优先_C#_Entity Framework_Mapping_Ef Code First_Relational Database - Fatal编程技术网

C# EF代码中自引用实体的映射优先

C# EF代码中自引用实体的映射优先,c#,entity-framework,mapping,ef-code-first,relational-database,C#,Entity Framework,Mapping,Ef Code First,Relational Database,在我的数据库中,我有一个表类别,列Id、CategoryName、ParentCategoryId,其中ParentCategoryId对Category.Id有一个约束 我首先使用实体框架代码,其中实体如下所示: public class Category { public long Id { get; private set; } public string CategoryName { get; private set; } public long? ParentCate

在我的数据库中,我有一个表类别,列Id、CategoryName、ParentCategoryId,其中ParentCategoryId对Category.Id有一个约束

我首先使用实体框架代码,其中实体如下所示:

public class Category
{
   public long Id { get; private set; }
   public string CategoryName { get; private set; }
   public long? ParentCategoryId { get; private set; }
   public Category ParentCategory { get; private set; }       
   public virtual ICollection<Category> SubCategories { get; private set; }
}
因此,如果我添加以下内容,它似乎需要导航属性:

 public ICollection<Category> Category1 { get; private set; }
 public long? Category2Id { get; private set; }
 public Category Category2 { get; private set; }
public ICollection Category1{get;private set;}
公众长?Category2Id{get;private set;}
公共类别类别2{get;private set;}
查询是有效的

但当然,我不希望使用Category1和Category2属性,我希望使用ParentCategory和SubCategories属性


如何让代码首先使用正确的导航属性?

我想我找到了它,我在onmodel创建操作中添加了以下内容:

 modelBuilder.Entity<Domain.Entities.Category>().HasOptional<Category>(c => c.ParentCategory).WithMany().Map(m => m.MapKey(new string[] { "Id", "ParentCategoryId" }));
modelBuilder.Entity().has可选(c=>c.ParentCategory).WithMany().Map(m=>m.MapKey(新字符串[]{“Id”,“ParentCategoryId”});

现在ParentCategory和SubCategories属性起作用了(我可以删除Category1和Category2)。不知道为什么子类别可以工作…

您的POCO类应该是这样的

public class Category
{
   public long Id { get; private set; }
   public string CategoryName { get; private set; }
   public long? ParentCategoryId { get; private set; }
   public virtual Category ParentCategory { get; private set; }       
   public virtual ICollection<Category> SubCategories { get; private set; }
}

public class CategoryConfiguration : EntityTypeConfiguration<Category>
{
    public CategoryConfiguration()
    {
        this.HasKey(x => x.Id);

        this.HasMany(category => category.SubCategories)
            .WithOptional(category => category.ParentCategoryId)
            .HasForeignKey(course => course.UserId)
            .WillCascadeOnDelete(false);
    }
}
公共类类别
{
公共长Id{get;private set;}
公共字符串CategoryName{get;private set;}
public long?ParentCategoryId{get;private set;}
公共虚拟类别ParentCategory{get;private set;}
公共虚拟ICollection子类别{get;private set;}
}
公共类类别配置:EntityTypeConfiguration
{
公共类别配置()
{
this.HasKey(x=>x.Id);
this.HasMany(category=>category.SubCategories)
.WithOptional(类别=>category.ParentCategoryId)
.HasForeignKey(课程=>course.UserId)
.WillCascadeOnDelete(假);
}
}

实体框架6处理此问题。您只需确保[Key]注释用于标识主键。不确定它是否适用于虚拟关键字

 public class Category
{
   [Key]
   public long Id { get; private set; }
   public string CategoryName { get; private set; }
   public long? ParentCategoryId { get; private set; }
   public Category ParentCategory { get; private set; }       
   public ICollection<Category> SubCategories { get; private set; }
}
公共类类别
{
[关键]
公共长Id{get;private set;}
公共字符串CategoryName{get;private set;}
public long?ParentCategoryId{get;private set;}
公共类别ParentCategory{get;private set;}
公共ICollection子类别{get;private set;}
}

正如您所建议的,我尝试了'modelBuilder.Entity().HasMany(category=>category.SubCategories)。WithRequired(category=>category.ParentCategory)。HasForeignKey(category=>category.Id)。WillCascadeOnDelete(false);'事实上,这也是可行的。我更喜欢你的方法,它是强类型的!谢谢或者它应该是可选的,而不是必需的…?@Lud,是的,很抱歉它应该是可选的。有趣的是,我们很难把它放到另一个方向。e、 g..has可选(x=>x.Parent)。有许多(c=>c.Children),没有给出期望的结果,但是这很有效,谢谢。这里的问题非常类似:
 public class Category
{
   [Key]
   public long Id { get; private set; }
   public string CategoryName { get; private set; }
   public long? ParentCategoryId { get; private set; }
   public Category ParentCategory { get; private set; }       
   public ICollection<Category> SubCategories { get; private set; }
}