简体   繁体   English

没有嵌套属性的实体框架包含

[英]Entity Framework Include without nested properties

I have two related entities called DataTag and TagSource that look like the following: 我有两个相关的实体,分别称为DataTag和TagSource,如下所示:

public class DataTag : BaseModel
{
    [Column("DataTagId")]
    public override Guid ID { get; set; }
    public string Tag { get; set; }

    public Guid TagSourceId { get; set; }
    public TagSource TagSource { get; set; }
}

public class TagSource : BaseModel
{
    [Column("TagSourceId")]
    public override Guid ID { get; set; }
    public string Description { get; set; }
    public bool IsInternal { get; set; }
    public string Source { get; set; }

    public ICollection<DataTag> DataTags { get; set; }
}

I am allowing the user to Include the navigation properties through the url like "/api/DataTags?Include=TagSource". 我允许用户通过“ / api / DataTags?Include = TagSource”之类的网址包含导航属性。 The problem is when I include the TagSource, it also includes the collection of DataTags in that object which I don't want unless the user specifies it (For example "/api/DataTags?Include=TagSource.DataTags". Is there any way to stop that property from being loaded when I include the TagSource? I have tried making the properties virtual and turning lazy loading off globally but that didn't work. The reason I haven't marked them virtual is because I am using AutoMapper and I only want to include the navigation properties that the user specifies. 问题是,当我包含TagSource时,它还会在该对象中包含我不希望的DataTag集合,除非用户指定(例如,“ / api / DataTags?Include = TagSource.DataTags”。)要在我包含TagSource时阻止该属性被加载?我试图使属性变为虚拟并在全局范围内关闭延迟加载,但这没有用。我未将其标记为虚拟的原因是因为我使用的是AutoMapper,只希望包括用户指定的导航属性。

As in the comments you need to create a DTO object. 如注释中所示,您需要创建一个DTO对象。 There is a good article here detailing how to do this with WebAPI 这里有一篇很好的文章,详细介绍了如何使用WebAPI进行此操作

http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5 http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5

Edit. 编辑。

The problem with this is you will need a lot of different DTO objects for each possible outcome which could become messy. 问题在于,对于每种可能导致混乱的结果,您将需要许多不同的DTO对象。 If your return type is JSON you can add this attribute to your properties: 如果返回类型为JSON,则可以将此属性添加到属性中:

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

Firstly : Apologies for my English. 首先:对我的英语表示歉意。

Secondly : I had the same issue with a code first database model that creates foreign keys this way : public virtual Collection<Object> Objects {get; set;} 其次:对于以这种方式创建外键的代码优先数据库模型,我也遇到了同样的问题: public virtual Collection<Object> Objects {get; set;} public virtual Collection<Object> Objects {get; set;}

and I found a workaround by setting the property setter as private: 我通过将属性设置器设置为私有找到了解决方法:

public virtual Collection<Object> Objects {get; private set;}

Then the EF cannot populate the Objects collection because with a private set you can only assign a value in constructors. 然后,EF无法填充Objects集合,因为使用私有集,您只能在构造函数中分配一个值。

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

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