简体   繁体   English

EF&Linq无法检索相关数据

[英]EF & Linq unable to retrieve related data

I have the current entity class Timesheet : 我有当前的实体类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
}

When executing this simple query, 执行此简单查询时,

var query = db.Timesheets.AsQueryable();
var data = query;
return data;

I get the following results (note CurrentProject and CurrentAccount ): 我得到以下结果(注意CurrentProjectCurrentAccount ):

<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>

However, when trying to explicitly load related objects, 但是,当尝试显式加载相关对象时,

var query = db.Timesheets.AsQueryable();
var data = query.Include(n => n.CurrentProject);
return data;

I get a serialization error: 我收到序列化错误:

The 'ObjectContent 1' type failed to serialize the response body for content type 'application/xml; “ ObjectContent 1”类型未能序列化内容类型“ application / xml”的响应主体; charset=utf-8' 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. <ExceptionMessage>输入“System.Data.Entity.DynamicProxies.Timesheet_B61B0CCFFB38C448231EBA3EF4D3D57C851AD2AE97FAA0BF8AA7F175D4C507D6”与数据合同名“Timesheet_B61B0CCFFB38C448231EBA3EF4D3D57C851AD2AE97FAA0BF8AA7F175D4C507D6: http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies ”不期望。 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. 考虑使用DataContractResolver或将任何静态未知的类型添加到已知类型的列表中-例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型的列表中。 </ExceptionMessage>

I ran a similar query using LINQPad and got correct results with all the related objects so I don't know what's wrong here. 我使用LINQPad进行了类似的查询,所有相关对象均获得了正确的结果,所以我不知道这是怎么回事。

I'm a n00b here and can really use your help! 我在这里n00b,可以真正使用您的帮助!

It's almost certainly due to a navigation property from Project back to the Timesheet. 几乎可以肯定,这是由于从Project到Timesheet的导航属性。

When Timesheet is being serialized, so is the CurrentProject property. 序列化时间表时,CurrentProject属性也是如此。 That, in turn, references the Timesheet, resulting in a circular reference. 依次引用时间表,从而产生循环引用。

Decorate the Timesheet navigation property on Project class to prevent it being serialized: [JsonIgnore] . 装饰Project类的Timesheet导航属性以防止对其进行序列化: [JsonIgnore] You'll have to do the same with the Account class if you .Include() that, too. 如果还包括.Include(),则必须对Account类执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM