【发布时间】:2017-02-14 15:56:09
【问题描述】:
我有一个 Parent 和 Child 类/表,我正在使用 Fluent API 来配置我的映射。这些表是在每个表中的非主键字段上连接的,所以根据我读过的内容,我无法在实体框架中配置连接。因此,我将手动加载我的 Parent.Children 属性 (parent.Children = (from x in context.Children...)
但是,我的 Parent 映射出现异常。我的课看起来像
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; }
}
当我尝试查询 context.Parents 时,我得到一个异常,因为 Entity Framework 生成的 SQL 正在尝试将 Parent 连接到 Child 并在 Child 上寻找一个名为 Child_ParentPrimaryKey 的属性。
如果我添加 modelBuilder.Entity<Parent>().Ignore(p => p.Children); 我得到异常 System.NotSupportedException: The specified type member 'Packages' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
如何将Children 保留为Parent 上的非映射属性而不会出错?
【问题讨论】:
-
Ignore在这种情况下应该可以工作。我相信映射没问题,错误在查询中。NotSupportedException听起来您在查询中使用了未映射的集合导航属性Packages,并且其方式不受支持。你能显示你正在使用的完整查询吗? -
非常感谢@V.Leon!这个问题让我发疯,你的评论指出了这个问题。我在查询中使用了
Children导航属性。我删除了它,它现在可以工作了。
标签: c# entity-framework