实体框架应该只返回父表而不是子表 [英] Entity framework should return only parent table not child table

查看:65
本文介绍了实体框架应该只返回父表而不是子表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想只返回父表数据,但默认情况下,db context返回带有子表数据的父表的数据。可以创建另一个模型并设置父表数据,但查找只返回父表数据的实体。



让我知道直接代码我怎么才能得到父表数据。





预付款,





问候,

Arun RV



我的尝试:



Hi All,

I want to return only the parent table data but by default db context return data with parent table with child table data. Can create another model and set the parent table data but looking for entity returning only parent table data.

Let me know the direct code how can I get only the parent table data.


Advance thanks,


Regards,
Arun RV

What I have tried:

_dbContext.TestResults.ToArray();

推荐答案

好吧。实体框架是一个存储库,它将加载关系,通常是在访问它们时。因此,如果您不访问相关的东西,它将不会被加载。



但简单的方法是简单地从你的Linq返回一个新类型。这可能是一个预定义的传输对象,或者只是在运行中声明。



例如



_dbContext.MyMainEntityTable.First(a => a.id = whatnot).select(s => new NotTheChildObject {WantedValue = s.Value1,AnotherWantedValue = s.Value2});



这将返回一个包含两个成员的新类NotTheChildObject:WantedValue,AnotherWantedValue
Well. Entity framwork is a repository which will load relations, typically when they're accessed. So if you don't access the related thing it won't get loaded.

But the easy way is to simply return a new type from your Linq. Of cause this can be a predefined transport object populated, or simply declared on the fly.

for instance

_dbContext.MyMainEntityTable.First(a => a.id = whatnot).select(s => new NotTheChildObject{ WantedValue = s.Value1, AnotherWantedValue = s.Value2 });

this will return a new class NotTheChildObject with two members: WantedValue, AnotherWantedValue


这篇关于实体框架应该只返回父表而不是子表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆