0

Here is my current problem. Lets say I have a DBConext for Customers called CustomerContext and on for Employees called EmployeeContext. I have them in two different DBContexts to keep it smaller and simplified. When I want to create a new customer, I call my Customers BLL(Business Logic Layer) which will in turn create a new instance of the CustomerContext. Now that context will be passed to a other related BLLs to verify the information and add their own records to the context to be inserted. when a context is passed into a BLL, that BLL will not save the changes to the context, the Parent level procedure will do that.

This works fine except when I pass the context to the Notes BLL. Here If I define the context as CustomerContext I am okay, but I need to also use this with EmployeeContext. How can I pass the Context in and determine which one to use at runtime? I tried passing it in as an object, but then if I try to add it to the table, I get object does not contain a definition for Note. Any suggestions would be greatly appreciated.

Here is a sample of what I want it to do, but I get an error on the moContext.Notes.Add(oNote); line, because its an object and not the context. moContext could be either CustomerContext or EmployeeContext.

object moContext;
bool mbContextCreatedLocal;

public NotesDAL(ref object pContext)
{
    moContext = pContext;
    mbContextCreatedLocal = false;
    }

public void InsertNote(Note pNote)
{
    Note oNote = null;
    oNote = new Note()
    {
        Note = pNote.Note.Trim(),
        NoteCategoryID = pNote.NoteCategoryID,
        Title = (string.IsNullOrEmpty(pNote.Title) ? null : pNote.Title.Trim()),
    };

    moContext.Notes.Add(oNote);

    if (mbContextCreatedLocal )
    {
        moContext.SaveChanges();
    }
}
  • 1
    EF was not designed to use multiple context. One context = one connection so you cannot even use a transaction among different context. I'd suggest you to use one context and abstract at a higher level – Camilo Terevinto Aug 21 '17 at 15:27
  • @CamiloTerevinto at least for SQL Server, you **can have a single transaction for multiple `DbContext`**. If you have two `DbContext` pointing to different databases in **the same server**, you can use a single transaction (I don't know if this is possible for different servers). As per what I read [here](https://stackoverflow.com/questions/1063502/implementing-transactions-over-multiple-databases), you can even use different DBMS and still use transactions. – Alisson Reinaldo Silva Aug 21 '17 at 16:11
  • Well, it's possible to use as many `DbContext` as you want in an application, just keep in mind every `DbContext` can connect to one database. You can actually have two or more `DbContext` connecting to the same database, or each one connecting to different servers, or even using different DBMS (e.g Postgres and MySQL). But is still not clear what is wrong, can you share your code? It looks you have a problem with your design. – Alisson Reinaldo Silva Aug 21 '17 at 16:22
  • I added to the main post. – Ryan Orzolek Aug 21 '17 at 17:03
  • Why dont you declare your moContext as ``DbContext``, the base class of both your contexts? Than you could call ``moContext.Set(pNote.GetType()).Add(oNote);`` ?!? – Rand Random Aug 21 '17 at 17:36
  • Btw, are you really asking why you get the message that your moContext doesnt have a definition of ``.Notes`` when you declared it as object? Is - propably - this message ``'object' does not contain a definition for 'Notes' and no extension method 'Notes' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)`` unclear to you? – Rand Random Aug 21 '17 at 17:41
  • No I understand why that error is coming up. I just am not sure how to deal with the possibility of multiple contexts. i will try your suggestion. – Ryan Orzolek Aug 21 '17 at 17:52

0 Answers0