我有一个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
时,我得到一个例外,因为实体框架生成的SQL正在尝试将Parent
加入Child
并正在{{1}上查找属性调用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
作为非映射属性而不会出错?
答案 0 :(得分:1)
您可以通过添加[NotMapped]
属性
using System.ComponentModel.DataAnnotations.Schema;
public class Parent {
// ...
[NotMapped]
public List<Child> Children { get; set; }
}
答案 1 :(得分:0)
感谢所有回复的人。您的评论让我意识到问题在于我在LINQ查询中使用了Parent.Children
属性。我删除了它,它现在正在工作。