EF & Linq 无法检索相关数据
EF & Linq unable to retrieve related data
我有当前实体 class Timesheet
:
public class Timesheet
{
#region Properties
public int Id { get; set; }
public DateTime TransactionDate { get; set; }
public int ProjectId { get; set; }
public int AccountId { get; set; }
[MaxLength(50)]
public string Supplier { get; set; }
#endregion
#region Navigation Properties
[ForeignKey("ProjectId")]
public virtual Project CurrentProject { get; set; }
[ForeignKey("AccountId")]
public virtual Account CurrentAccount { get; set; }
#endregion
}
执行这个简单查询时,
var query = db.Timesheets.AsQueryable();
var data = query;
return data;
我得到以下结果(注意 CurrentProject 和 CurrentAccount):
<Timesheet>
<AccountId>33</AccountId>
<CurrentAccount i:nil="true"/>
<CurrentProject i:nil="true"/>
<Id>1</Id>
<ProjectId>1064</ProjectId>
<Supplier>Test Supplier</Supplier>
<TransactionDate>2016-02-27T00:00:00</TransactionDate>
</Timesheet>
但是,当尝试显式加载相关对象时,
var query = db.Timesheets.AsQueryable();
var data = query.Include(n => n.CurrentProject);
return data;
我收到一个序列化错误:
The 'ObjectContent 1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'
<ExceptionMessage>
Type 'System.Data.Entity.DynamicProxies.Timesheet_B61B0CCFFB38C448231EBA3EF4D3D57C851AD2AE97FAA0BF8AA7F175D4C507D6' with data contract name 'Timesheet_B61B0CCFFB38C448231EBA3EF4D3D57C851AD2AE97FAA0BF8AA7F175D4C507D6:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.</ExceptionMessage>
我 运行 使用 LINQPad
进行了类似的查询,并得到了所有相关对象的正确结果,所以我不知道这里出了什么问题。
我是新手,真的需要你的帮助!
这几乎可以肯定是由于 属性 从 Project 返回到 Timesheet 的导航。
当时间表被序列化时,CurrentProject 属性 也会被序列化。反过来,引用时间表,导致循环引用。
修饰项目 class 上的时间表导航 属性 以防止其被序列化:[JsonIgnore]
。如果您也 .Include() 的话,您必须对帐户 class 执行相同的操作。
我有当前实体 class Timesheet
:
public class Timesheet
{
#region Properties
public int Id { get; set; }
public DateTime TransactionDate { get; set; }
public int ProjectId { get; set; }
public int AccountId { get; set; }
[MaxLength(50)]
public string Supplier { get; set; }
#endregion
#region Navigation Properties
[ForeignKey("ProjectId")]
public virtual Project CurrentProject { get; set; }
[ForeignKey("AccountId")]
public virtual Account CurrentAccount { get; set; }
#endregion
}
执行这个简单查询时,
var query = db.Timesheets.AsQueryable();
var data = query;
return data;
我得到以下结果(注意 CurrentProject 和 CurrentAccount):
<Timesheet>
<AccountId>33</AccountId>
<CurrentAccount i:nil="true"/>
<CurrentProject i:nil="true"/>
<Id>1</Id>
<ProjectId>1064</ProjectId>
<Supplier>Test Supplier</Supplier>
<TransactionDate>2016-02-27T00:00:00</TransactionDate>
</Timesheet>
但是,当尝试显式加载相关对象时,
var query = db.Timesheets.AsQueryable();
var data = query.Include(n => n.CurrentProject);
return data;
我收到一个序列化错误:
The 'ObjectContent 1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'
<ExceptionMessage>
Type 'System.Data.Entity.DynamicProxies.Timesheet_B61B0CCFFB38C448231EBA3EF4D3D57C851AD2AE97FAA0BF8AA7F175D4C507D6' with data contract name 'Timesheet_B61B0CCFFB38C448231EBA3EF4D3D57C851AD2AE97FAA0BF8AA7F175D4C507D6:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.</ExceptionMessage>
我 运行 使用 LINQPad
进行了类似的查询,并得到了所有相关对象的正确结果,所以我不知道这里出了什么问题。
我是新手,真的需要你的帮助!
这几乎可以肯定是由于 属性 从 Project 返回到 Timesheet 的导航。
当时间表被序列化时,CurrentProject 属性 也会被序列化。反过来,引用时间表,导致循环引用。
修饰项目 class 上的时间表导航 属性 以防止其被序列化:[JsonIgnore]
。如果您也 .Include() 的话,您必须对帐户 class 执行相同的操作。