ADO.NET 实体框架 - 如何仅从一个表中选择数据(并忽略其他表)?
背景是我所在的团队刚刚开始使用 EntityFramework;首先我们设计数据库,将所有表关系、外键等放置到位;然后通过 Visual Studio 添加一个新的 ADO.NET 实体数据模型,我们会自动生成代表整个数据库的 edmx 文件!
现在我关注两个为所有下拉列表和查找列表提供数据的表;
TLookupDomain (domainID, domainName, domainDesc )
TLookup (lookupID, domainID, lookupCode, lookupDisplay, lookupDesc, sortOrder)
关系是从左到右的 1-M:
TLookupDomain -< TLookup-< TOther(+另外 30 个左右的其他表)
因此,lookupID 是多达 30 个表的外键;
IQueryable<TLookup> qList = from l in ctx.TLookups
where l.domainID == 24
select l;
foreach (TLookup l in qList)
{
//do something.
System.Diagnostics.Debug.WriteLine("{0}\t{1}", l.lookupCode, l.lookupDisplay);
foreach (TOther f in l.TOthers)
{
System.Diagnostics.Debug.WriteLine("{0}\t{1}", f.feeAmount, f.feeDesc);
}
}
当我执行上面的 LINQ 时,我得到了 TLookup 表的所有字段(这是公平的),但是也获取了链接到它的 30 个左右的表的数据,即使我对另一个表的数据不感兴趣至此,我将在 LINQ 获取数据后立即丢弃所有数据。
我有两个问题: Q.1) 我可以以某种方式修改上面的 LINQ 查询,或者告诉 EntityFramework 不要从其他 30 个链接表中获取数据吗?
Q.2) 使用一个 edmx 文件对整个数据库进行建模是否“正确”? (对我来说听起来很狡猾)。
Background is the team i'm in has just started using the EntityFramework; first we designed the database, put all the table relationships in place, foreign keys, etc; then thru visual studio add a new ADO.NET Entity Data Model, and auto-magically we get the generated edmx file representing the whole database !
Now i focus on two tables that provide data for all dropdowns and lookup lists;
TLookupDomain (domainID, domainName, domainDesc )
TLookup (lookupID, domainID, lookupCode, lookupDisplay, lookupDesc, sortOrder)
Relationship is 1-M going from left to right:
TLookupDomain -< TLookup -< TOther (+ another 30 or so other tables)
So lookupID is a foreign-Key to as many as 30 tables;
IQueryable<TLookup> qList = from l in ctx.TLookups
where l.domainID == 24
select l;
foreach (TLookup l in qList)
{
//do something.
System.Diagnostics.Debug.WriteLine("{0}\t{1}", l.lookupCode, l.lookupDisplay);
foreach (TOther f in l.TOthers)
{
System.Diagnostics.Debug.WriteLine("{0}\t{1}", f.feeAmount, f.feeDesc);
}
}
When i execute the above LINQ, i get all the fields for TLookup table (which is fair), BUT data is also fetched for the 30 or so tables that are linked to it, even though i am NOT interested in the other table's data at this point, and i am going to discard all data soon as LINQ fetches it.
Two Questions i have:
Q.1) Can i somehow modify the LINQ query above or tell the EntityFramework otherwise not to bother fetchin data from the 30 other linked tables ?
Q.2) is it "right" to have one edmx file that models the entire database? (sounds dodgy to me).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将模型的延迟加载配置为 true。关系应仅在导航时加载。您还可以拆分模型以避免过多不必要的关系。
Configure Lazy Load to true for the model. Relations should be loaded only upon navegation. You can also split the models to avoid too many unnecessary relations.
Linq-to-Entities 查询不会自动获取任何内容。导航属性的获取可以通过急切加载或延迟加载来执行。您没有使用预先加载,因为这需要在查询中调用
Include
(或单独调用ctx.LoadProperty
)。因此,如果您的数据被获取,那一定是由于默认情况下启用的延迟加载。一旦您访问代码中的导航属性,就会触发延迟加载。您还可以使用投影仅返回所需的数据。像这样的东西应该返回只读数据:
拥有一个或多个 EDMX 是常见的困境。使用单个 EDMX 使事情变得更加简单。如果您想了解如何使用多个 EDMX 并共享概念定义,请查看这两篇文章:第 1 部分,第 2 部分。
Linq-to-Entities queries do not fetch anything automatically. Fetching of navigation properties is performet either by eager or lazy loading. You are not using eager loading because that requires calling
Include
in query (orctx.LoadProperty
separately). So if your data are fetched it must be due to lazy loading wich is enabled by default. Lazy loading triggers once you access the navigation property in the code.You can also return only the data you need by using projections. Something like this should return readonly data:
Having one or more EDMX is common dilemma. Working with single EDMX makes things more simple. If you want to know how to use multiple EDMXs and share conceptual definitions check these two articles: Part 1, Part 2.