简体   繁体   English

实体框架-如何返回没有相关对象的单个实体

[英]Entity Framework - How to return single entity without related objects

Im using EF6, DB First, in a MVC project with LazyLoading turned off . 我在关闭LazyLoading的MVC项目中使用EF6,DB First。 However upon updating some of the entities properties, I found that upon updating an entities related objects in a function in the same context, that by the time I return the single entity, it also contains the related objects that I had updated. 但是,在更新某些实体属性后,我发现在同一上下文中更新功能中的实体相关对象时,到我返回单个实体时,它还包含我已更新的相关对象。 I would like to return the single entity only. 我只想返回单个实体。

So my question is, is there a better way to "clean off" the related entities other than creating a new context and retrieving the entity again? 所以我的问题是,除了创建新的上下文并再次检索该实体之外,还有没有更好的方法来“清除”相关实体? Or would you update the related entities in a different context? 还是会在不同的上下文中更新相关实体? Or is there a cleaner method than these? 还是有比这更清洁的方法?

Thanks in advance! 提前致谢!

Your issue is that you are expecting Entity Framework to behave in a way that it is not intended to do. 您的问题是您期望Entity Framework的行为方式并非预期的。 Based on your comments and question, it seems that in the same context you are touching a parent entity and one or more of its children entities. 根据您的评论和问题,似乎您在同一上下文中接触一个父实体及其一个或多个子实体。 As a result, the change tracker is putting all of that together for you. 结果,变更跟踪器会为您整合所有这些内容。

Your second issue is that your code return probably looks something like this: 第二个问题是您的代码返回可能看起来像这样:

public ActionResult Stuff(){
    //MyEntityFrameworkClass is an autogenerated Entity Framework 
    //object that represents a table in your database
    List<MyEntityFrameworkClass> items = db.MyEntityFrameworkClass.ToList();
    Return Json(items);
}

This is bad because you are returning everything in the database for each of these items including any of the related entities that have been attached. 这很糟糕,因为您将为数据库中的每一项返回所有内容,包括已附加的任何相关实体。 What happens in these instances: 在这些情况下会发生什么:

  • You don't need to return every single column 您不需要返回每一列
  • You want to return some auto calculated columns not included in the entity 您想要返回一些未包含在实体中的自动计算的列
  • You need values from multiple entities 您需要来自多个实体的值

This is where View Models (or Data Transfer Objects) come into play. 这就是视图模型(或数据传输对象)发挥作用的地方。 Create a model specifying EXACTLY what your client needs and nothing more. 创建一个模型, 确切说明客户的需求,仅此而已。

public class MyApiModel(){
   public string Name {get;set;}

   public int SomethingElse {get;set;}

   //Computed property in our view model
   //Lets say anything greater than 2 is valid
   public bool IsValid => SomethingElse > 2;
}

Now you should return a List (or whatever) of this type of object. 现在,您应该返回此类对象的列表(或任何其他形式)。

public ActionResult Stuff(){
    List<MyApiModel> items = db.MyEntityFrameworkClass.Select(x=>new MyApiModel{
          //Notice I am not setting the isvalid property
          //Its computed, class takes care of returning proper value
          Name = x.MyNameColumn, 
          SomethingElse = x.MyOtherColumn
    }.ToList();
    Return Json(items);
}

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

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