Multiple DataContext

Multiple DataContext

Old forum URL: forums.lhotka.net/forums/t/11090.aspx


sakt posted on Wednesday, January 25, 2012

All,

We are facing scenario where we need to fetch data from two different databases. We had data context set up in our design by loading the dbml file for one database. And fetch is happening in factory methods using this data context. Could someone please assist on how to handle scenario of multiple dbml files (i.e two databases)

Thanks and Regards,

Sakt

RockfordLhotka replied on Wednesday, January 25, 2012

You'd write code like this in your DataPortal_Fetch or DAL implementation invoked by DataPortal_Fetch:

      using (var ctx = ObjectContextManager<SqlDbEntities>.GetManager("SqlDbEntities"))
      {
        var data1 = (from r in ctx.ObjectContext.People
                   where r.Id == id
                    select r).First();
        // load business object properties from data1 object
      }
      using (var ctx = ObjectContextManager<SqlDbEntities>.GetManager("OtherEntities"))
      {
        var data2 = (from r in ctx.ObjectContext.OtherPeople
                    where r.Id == id
                    select r).First();
        // load business object properties from data2 object
      }

sakt replied on Thursday, January 26, 2012

Hi,

Thank you very much for the reply. In my case, we have dbml file for database1 in our data layer . Hence I understand that we would need to one more dbml file for other database2 and access the same using the code in Dataportal_Fetch.

Please note that my understanding is right as im newbie to the same. Also I would like to let you know that from the other database2, we are just going to get data from table. There will be no Add,update and delete operations involved.

Regards,

Sakt

Copyright (c) Marimer LLC