实体框架与多个EDMX [英] Entity Framework with multiple edmx

查看:89
本文介绍了实体框架与多个EDMX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我在我的数据库中的多个数据库模式,例如: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.

这篇关于实体框架与多个EDMX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆