locked
Looking for a simple example of querying across edmx models RRS feed

  • Question

  • Can someone point me to a simple example of how to query (via Linq to Entities in my case) across edmx models? Our model is very large and I'm sure we will be breaking it up logically somehow. If I know how to query across edmx boundaries, it will help me design the separations. Our database model is very normalized so we have all the necessary foriegn keys in place if that matters.
    Monday, March 7, 2011 1:23 AM

Answers

All replies

  • Do you mean having a single query pull data from two different EDMXes and join? If so, that's not supported. You'll need to do separate queries against each model and then join the results client-side with a linq-to-objects query.
     
       Cool tools for Linq-to-SQL and Entity Framework 4:
     huagati.com/dbmltools - Visual Studio add-in with loads of new features for the Entity Framework and Linq-to-SQL designers
     huagati.com/L2SProfiler - Runtime SQL query profiler for Linq-to-SQL and Entity Framework v4
    • Proposed as answer by Alan_chen Monday, March 7, 2011 7:26 AM
    • Unproposed as answer by Barry Etter Tuesday, March 8, 2011 12:40 PM
    Monday, March 7, 2011 1:38 AM
  • The impression I got from this post:

    http://social.msdn.microsoft.com/forums/en-us/adodotnetentityframework/thread/67274256-5729-4379-B203-C924C51F34D5

    was that it is possible to span edmx models, though I was not able to follow that thread. That's why I was looking for a sample.

    Monday, March 7, 2011 3:18 PM
  • There is a difference between splitting the same Data Model across multiple *.edmx files and creating separate Data Models with separate *.edmx files.

    In the first instance, the code generation will create a single ObjectContext from the multiple files that you can then query with LINQ to Entities without any problems.

    In the second instance, you cannot create a LINQ to Entities query that understands how to generate SQL that accesses both data stores since it's beyond the scope of the query generator to know that level of detail.

    As pointed out, you have to execute a query against one data store, then use the results to build a query to execute independently on the second data store.  If you try to include both ObjectContext instances in a single query, LINQ to Entities will throw an exception.

    Joe

    Monday, March 7, 2011 10:31 PM
  • There is a difference between splitting the same Data Model across multiple *.edmx files and creating separate Data Models with separate *.edmx files.

    In the first instance, the code generation will create a single ObjectContext from the multiple files that you can then query with LINQ to Entities without any problems.

    In the second instance, you cannot create a LINQ to Entities query that understands how to generate SQL that accesses both data stores since it's beyond the scope of the query generator to know that level of detail.

    As pointed out, you have to execute a query against one data store, then use the results to build a query to execute independently on the second data store.  If you try to include both ObjectContext instances in a single query, LINQ to Entities will throw an exception.

    Joe


    Thanks for clearing that up, Joe. Is the first scenario, spiltting the same data model across .edmx files, difficult? Is there a sample of that somewhere?
    • Edited by Barry Etter Tuesday, March 8, 2011 12:41 PM
    Monday, March 7, 2011 10:56 PM
  • Unfortunately, it is difficult and requires working against the XML of the .edmx itself.  This is about the only useful link that starts talking about steps to take that I found, though if you search the net a bit you'll find more out there:

    http://blogs.msdn.com/b/adonet/archive/2008/11/25/working-with-large-models-in-entity-framework-part-2.aspx

    • Marked as answer by Barry Etter Tuesday, March 8, 2011 12:40 PM
    Tuesday, March 8, 2011 1:00 AM