Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework with multiple edmx

Let's say I have in my database multiple db schema for example : HumanRessources and Inventory.

In each of those schema contains multiple tables. Do you usually split your DB into multiple edmx or usually just put everything in one single edmx?

I was thinking about creating a edmx for each schema, but wondering how this will impact a unitorwork pattern. Reading through some articles, the ObjectContext will be the unitofwork. By defining 2 edmx, I will end up with 2 ObjectContext : HumanRessourceContext and InventoryContext, meaning each will will be a unitofwork. What if I want all modification made to an entity in the humanressource and an entity in the inventorycontext to be ATOMIC, can this be achieve with the unitofwork pattern?

like image 666
pdiddy Avatar asked Sep 15 '10 13:09

pdiddy


1 Answers

While this isn't an endorsement of splitting up the database by schema into EDMX's, you can make the updates atomic by using a TransactionScope:

using(TransactionScope trans = new TransactionScope())
{
    using(HumanResources hr = new HumanResources())
    {
        //...

        hr.SaveChanges();
    }

    using(Inventory inv = new Inventory())
    {
        //...

        inv.SaveChanges();
    }

    trans.Complete();
}

Obviously you can rearrange your context objects however you like (if you need to use them both at the same time, for instance) and you can alter the transaction isolation level to whatever is appropriate, but this should give you what you need to know in order to make your database changes atomic.

like image 91
Adam Robinson Avatar answered Sep 23 '22 00:09

Adam Robinson