I am trying to migrate a project from an older Entity Framework using ObjectContext to DbContext. My problem is that ObjectSet<Garden> & EntityCollection<Flowers>
is now DbSet<Garden> & ICollection<Flowers>
and that my code needs to run dynamic queries against the tables and the entities navigational/related tables.
var flowers = Gardens.Where("it.Name = @name").First().Flowers.Where(blah);
LINQ is not an alternative - must be based on dynamically build strings (eSQL build by grids, typed by user etc. and used for Where, GroupBy, OrderBy). Only ObjectQuery<>
seems to have string/eSQL predicate.
How to do this the best way that also works on related/foreign key tables? Am I missing something here - because it seems like a hard thing for something that was very simple before?
I solved this by creating a T4 file that adds to the main context (query on tables) and to all entities (navigation/foreign key). Here is the simplified version:
// Get table as ObjectQuery
var myGarden = ((IObjectContextAdapter)this).ObjectContext.CreateObjectSet<Garden>("Gardens").First();
// myGarden is ObjectQuery with eSQL support
var foo = myGarden.Where("it.Works = true");
// Get the Flowers (navigation) from the Garden entity as ObjectQuery
var flowers = (ObjectQuery<Flower>)Entry(myGarden).Collection<Flower>("Flowers").Query();
// flowers is ObjectQuery with eSQL support
flowers.Where("it.AlsoWorks = true");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.