开发者

ADO.NET Entity Framework - How to select data from one Table only (and ignore other tables)?

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


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 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 (or ctx.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:

var query = from l in ctx.TLookups
            where l.domainId == 24
            select new 
              {
                l.lookupCode,
                l.lookupDisplay,
                l.TOthers
              };

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜