C# 非映射子实体的错误

C# 非映射子实体的错误,c#,entity-framework,C#,Entity Framework,我有一个Parent和Child类/表,我正在使用Fluent-API配置映射。这些表是在每个表中的非主键字段上联接的,因此根据我所读的内容,我无法在实体框架中配置联接。因此,我将手动加载我的Parent.Children属性(Parent.Children=(从context.Children中的x…) 但是,我的父类映射出现异常 public class Parent { // Unique primary key public int ParentPrimaryKey { ge

我有一个
Parent
Child
类/表,我正在使用Fluent-API配置映射。这些表是在每个表中的非主键字段上联接的,因此根据我所读的内容,我无法在实体框架中配置联接。因此,我将手动加载我的
Parent.Children
属性(
Parent.Children=(从context.Children中的x…

但是,我的
父类映射出现异常

public class Parent
{
   // Unique primary key
   public int ParentPrimaryKey { get; set; }

   // These two fields together compose the foreign key to join to Child
   public string ParentForeignKey1 { get; set; }
   public string ParentForeignKey2 { get; set; }

   public List<Child> Children { get; set; }
}

public class Child
{
   // Unique primary key
   public int ChildPrimaryKey { get; set; }

   // These two fields together compose the (non-unique) foreign key to join to Parent
   public string ChildForeignKey1 { get; set; }
   public string ChildForeignKey2 { get; set; }

}
公共类父类
{
//唯一主键
public int ParentPrimaryKey{get;set;}
//这两个字段一起构成要连接到子级的外键
公共字符串ParentForeignKey1{get;set;}
公共字符串ParentForeignKey2{get;set;}
公共列表子项{get;set;}
}
公营儿童
{
//唯一主键
public int ChildPrimaryKey{get;set;}
//这两个字段一起构成(非唯一)外键以连接到父级
公共字符串ChildForeignKey1{get;set;}
公共字符串ChildForeignKey2{get;set;}
}
当我尝试查询
context.Parents
时,我得到一个异常,因为Entity Framework生成的SQL正在尝试将
Parent
连接到
Child
,并且正在查找
Child
上名为
Child\u ParentPrimaryKey
的属性

如果我添加
modelBuilder.Entity().Ignore(p=>p.Children);
我会得到异常
System.NotSupportedException:指定的类型成员“Packages”在LINQ to Entities中不受支持。只支持初始值设定项、实体成员和实体导航属性。


如何将
子项
保留为
父项
上的未映射属性而不出错?

您可以通过添加
[notmap]
属性从EF映射中排除属性:

using System.ComponentModel.DataAnnotations.Schema;
public class Parent {
   // ...

   [NotMapped]
   public List<Child> Children { get; set; }
}
使用System.ComponentModel.DataAnnotations.Schema;
公共类父类{
// ...
[未映射]
公共列表子项{get;set;}
}

感谢所有回复的人。您的评论让我意识到问题在于我在LINQ查询中使用了
Parent.Children
属性。我删除了该属性,它现在可以工作了。

难道
Ignore
不应该做同样的事情吗?我正在尝试将我的模型分离到它们自己的项目中,而不是让它独立于它们吗在实体框架上。实际上,
Ignore
也应该起作用。您是否显示了产生错误的实际代码?因为您收到的错误消息谈到了一个成员“包”这是不支持的。您是否在
或其他可能阻止EF映射的自定义C#上有特殊的getter?抱歉,实际代码中的
属性对应于此代码中的
子类
属性。在实际代码中,
公共列表包{get;set;}
顺序
对应于这里的
父项
忽略
在这种情况下应该可以工作。我相信映射是正确的,错误在查询中。
不支持异常
听起来像是在查询中使用未映射的集合导航属性
支持。你能显示你正在使用的完整查询吗?非常感谢@V.Leon!这个问题让我抓狂,你的评论指出了问题所在。我在查询中使用了
儿童导航属性。我删除了它,现在它正在工作。