score:0
I don't know if you've already figured this out, but merging DataContexts is not a good idea. A big part of the reason has to do with the change tracking that is built into the DataContext object. If you were able to keep the entity objects separate so that the changes only affect one DataContext at a time with no overlap, you could get it to work, but it just seems like so much trouble for so little payoff.
You could implement a repository pattern, but again, you still have the problem with one DataContext not recognizing objects created with another DataContext. The project that I am currently working uses only one DataContext. The database has about 75 tables now...I've got one-to-ones, one-to-manys, and many-to-many relationships and I have yet to run into serious performance or implementation issues using only a single DataContext. Using a facade pattern may work, but again, you need to ask yourself if maintaining multiple DataContexts that cannot directly interact is worth the effort.
For example, let's say that you have a Customer table and Orders table. The Customer table resides in one DataContext and the Orders table resides in the other. The relationship is one Customer to zero to many orders. Because you have them in separate DataContexts, I don't believe you can directly refer to the child entity object in your query. So, to get the orders for a particular customer, you may be forced to do this:
var orders = DC2.Orders.Where(a => a.Customer_ID == (DC1.Customers.Where(a => a.Customer_ID == 7).Customer_ID);
instead of this:
var orders = DC.Customers.Where(a => a.Customer_ID == 7).Select(a => a.Orders);
score:0
We achieved this by using inheritance within DataContexts, we use EF5, Code-First and MVC4 for the fron-end web projects.
We have a common DataContext to encapsulate all the common tables in our database
public partial class CommonDataContext : DbContext
{
//Your code
}
And multiple specialized data contexts that use their own tables + the common tables
public partial class Application1Context : CommonDataContext
{
//Your code
}
Each one of these data contexts are in separate projects, inside the same solution. Hope this help.
score:1
I'm not aware of anyway to achieve this progmatically.
What you might want to look at is using design-patterns to achieve this goal. By using patterns along with say a repository pattern then you can define your base interface repository that all of the client repositorys will inherit from. Then for each client repository you extend that with their specific needs.
score:2
I would use a Facade Pattern. Create a Facade Context which would abstract the underlying DataContext from the user. You could inherit from Default DataContext if you want and override the methods. Within the overrides, you could pass it to the appropriate DataContext.
Source: stackoverflow.com
Related Query
- Using Linq to build a graph class; can you make this code look better?
- How can you handle an IN sub-query with LINQ to SQL?
- Can you use LINQ types and extension methods in IronPython?
- How can you do custom sorting in LINQ with null always on the end?
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python?
- How can you update a Linq Expression with additional parameters?
- Where can I view LINQ source code?
- How can you see the sql that is causing an error on SubmitChanges in LINQ to SQL?
- Can you advise me a resource with LINQ/lambda code exercises?
- LINQ Source Code Available
- How can I switch that code to use LINQ
- How can I combine this code into one or two LINQ queries?
- Can you set/update a value in real-time within a LINQ statement during iteration?
- How can I merge two Linq IEnumerable<T> queries without running them?
- linq - how do you do a query for items in one query source that are not in another one?
- In LINQ to Entities, How can you order like follows?
- Can you use linq to see if two IEnumerables of data contain any common entries?
- How can I write the following code more elegantly using LINQ query syntax?
- How can I further simplify this piece of LINQ code
- Merge duplicate data without affecting others in LINQ code
- How can I code an outer join using LINQ and EF6?
- Can you use LINQ extension method operators in an ASP.NET databinding expression?
- Can you use LINQ with in memory objects rather than SQL Server queries to improve performance?
- How can you retrieve all records of certain subtype dynamically with Linq to Entities?
- Can you use LINQ or lambdas to perform matrix operations?
- Can you set a property in a linq query before selecting the item?
- How can I merge two outputs of two Linq queries?
- Can you convert this Linq statement into Lambda without using join statements?
- How can I code a Linq query to do an upward Include?
More Query from same tag
- Convert Func<T, string>[] into Func<T, string[]>?
- LINQ query grouping without duplication of data
- Applying expression functions
- Reading XML file using Linq
- Convert string to int in an Entity Framework linq query and handling the parsing exception
- How to validate LINQ queryable without database
- List<int> except(AnotherList<int>) not return correct results
- How to create column and row wise json structure?
- Linq query averages
- LINQ Lambda, need to query to a very confuse database, how should I do it?
- LINQ Any() not working as expected
- Linq Order by when column name is dynamic and pass as a string to a function
- How to make the Linq2Sql understand custom types?
- How to find string in Dictionary list and get its pair integer value
- Get different rows in two typed DataTables with different schema
- Parsing a text File using Linq
- How to take elements from range with lambda expressions and linq?
- Search query using .Contains() for parameters that combine two columns
- First / FirstOrDefault not working as I would expect
- Generic LINQ extension for calculating weighted average in Visual Basic 2010
- Translate a bunch of booleans to a list of enums using LINQ
- Ajax Query Dropdown not populating
- LINQ to XML to read Hierarchal XML Document
- Hierarchical list based on parent_id using C# / LINQ
- Sorting a list + detecting at least 5 directly following items
- Changing a piece of code into expressions
- Returning proper type after using OrderBy()
- Nested foreaches into a chained linq expression
- LINQ sum with grouping, but also get line item value before grouping
- With Linq search the words that start from H to L