实体框架与多个EDMX多个、实体、框架、EDMX

2023-09-03 12:47:54 作者:生得再平凡也是限量版

比方说,我在我的数据库中的多个数据库模式,例如:HumanRessources和库存

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

在每个这样的架构包含多个表。你通常会分裂您的数据库为多个EDMX或通常只是把所有东西都在一个单独的EDMX?

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?

我在想创造一个EDMX对每个方案,但不知道这将如何影响一个unitorwork模式。通过阅读一些文章,该ObjectContext的将是的UnitOfWork。通过定义2 EDMX,我将结束与2的ObjectContext:HumanRessourceContext和InventoryContext,这意味着每次都会将是一个的UnitOfWork。如果我想在humanressource的实体,并在inventorycontext实体所做的所有修改是原子,会是这样的的UnitOfWork模式实现?

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?

推荐答案

虽然这不是分裂由模式的数据库到EDMX的的认可,可以进行更新原子通过使用的TransactionScope

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.