Sunday 15 March 2015

c# - How to have Entity Framework navigation property query filter in database by default? -


The DBKnext API has set the navigation properties in the iSelloplege (for the * expiration of the association). The usual way to get a queryable object (like you want a count) seems like

  int count = dbcontext.Entry (entry). Collection (c => c.navprop). () .ount ();   

But it is uncomfortable if you want to filter in DB more often. More importantly, it is also easy to forget. If someone has said accidentally

  int count = entry.navprop.Count ();   

Then it receives all data on the server and counts there, which is slow.

The same thing is true for EntityCollection type, which uses ObjectContext by default.

  int count = entry.navprop.CreateSourceQuery (). Calculation ();   

Is there a way to set in a model or elsewhere that the default collection type for the navigation property is an IQueryable or ObjectCag or any type of queryable type?

Note that this is just a real problem for navigation properties and in context the real object and the DBST item are queryable

I have come up with a solution that solves most of my problems What I was doing, object Konnect was using API and my model and navigation property which was private (received And for setting) Was taking a long time for the gram. You can do this in the ADMX file by right-clicking on the Nm property.

Then I created a partial class file for that class which had a private navigation property and added anything to it.

  Public object & lt; Neopropypeed & gt; NavPropName {if (privateNavProp! = Null) // Lazy Loading or some PersonalNowProp when idle Cretesource (); And tap return; }}   

Any user of the class can now try to pull up all the Navprop items, which are many. And instead of remembering to call CreateSourceQuery every time, it's easy to do a query on AOV Props.

I did not add a setter because that navigation property is read only in my application. I'm sure there is a way to fit this method, but I do not have enough information about the Object Project API, to say how to do it.

I had just finished doing this for the navigation properties, which could be a tonne figure behind them, because the performance has not had any effect except the other ones.

EDIT: There is a drawback to making it private that I later went on is that I can not do now

  db.EntryTable.OrderBy (e = & Gt; e.privateNavProp .Count ())   

Even if it was smart enough not to get all things

No comments:

Post a Comment