views:

115

answers:

2

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?

+3  A: 

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.

Adam Robinson
ok thanks, I was trying to avoid transactionscope. So pretty much the only way to go is to have one single edmx that contains all the elements of the human ressource and the elements of the inventory, in this way the unitofwork pattern will work fine.
pdiddy
yep - nice post Adam. Beat me to it :)
Pure.Krome
@pdiddy: Why are you trying to avoid TransactionScope?
Adam Robinson
I was just under the impression with the UnitOfWork pattern that I can avoid the transactionscope, since everything done on the object will be sent in one signe transaction anyway when calling savechanges. So I began wondering if I had 2 context, how will the unitofwork pattern works, but I see that I'll have to use a transactionscope to make the 2 context atomic which makes sense. I'm new to this unitofwork pattern so I'm just trying to understand it.
pdiddy
+1  A: 

If your Inventory and HumanResources tables don't have any relationships between them, splitting up the tables into two edmx files is fine, though I don't know what benefit it would offer. If they do have direct or indirect relationships, you will run into problems trying to use those relationships. The simplest solution is to use a single EDM.

Dave Swersky
It was more about structure, have it organized in my project ... having everything in one single edmx, lets say I have like 50 tables ... in one single edmx .. its kind of heavy just opening it in the designer can be confusing because there is so much .... but if it will bring me problems, Id rather have it one single edmx then. Thanks
pdiddy