实体框架 4.1:可以从包含多个表的数据上下文中保存单个表吗?

发布于 2024-11-25 00:28:21 字数 628 浏览 2 评论 0原文

我有一个先有鸡还是先有蛋的问题,这很简单,所以我想我会问保存聚合根的正常模式是什么,其中所有主键都是身份字段。

我有一个典型的联系人实体:

 Customer {
      HomeAddress {
      }
      WorkAddress {
      }
 }

两个地址都存储在地址表中,唯一的主键是身份字段。我们相互检查所有字段以保留唯一的地址记录。

问题是:

  1. 我需要检索地址标识字段来连接外键,因此仅当地址记录唯一时,我才会在保存客户记录之前保存地址记录,否则我会加载现有地址。
  2. 如果地址与客户位于同一个 DC 中,则客户保存得太早(并非所有记录都已设置)。
  3. 如果地址位于单独的 DC 中,则它不会连接到拥有自己的 DC 的客户记录,因为您无法拥有与两个 DC 关联的实体(无法在一个 DC 中打开,然后保存在另一个 DC 中。)

所以我的想法是,我需要为每个地址建立一个单独的存储库,然后在另一个 DC 中再次单独加载该地址,从而对数据库中的相同信息。

有没有办法将记录部分保存在实体框架 4.1 的数据上下文/容器中?例如,在仍处于同一 DC 中的情况下自行保存地址?

I have a chicken and egg problem, it's trivial, so I thought I would ask what's the normal pattern to save an aggregate root where all the primary keys are identity fields.

I have a typical contact entity:

 Customer {
      HomeAddress {
      }
      WorkAddress {
      }
 }

where both addresses are stored in the address table and the only primary key is the identity field. We check all fields against each other to keep unique address records.

Here's the problem:

  1. I need to retrieve the Address identity field to hookup the foreign keys, so I save the Address record prior to saving the Customer record only if it's unique, otherwise I load that existing Address.
  2. If Address is in the same DC as Customer, then customer saves too prematurely (not all records are set.)
  3. If Address is in a separate DC, then it doesn't hookup to the Customer record that has it's own DC because you cannot have an entity associated with two DCs (can't open in one, then save in another.)

So my thinking is that I would need a separate repository for every Address, then separately load the address again in the other DC, making redundant calls to the database for the same information.

Is there a way to partially save records in a data context / container in Entity Framework 4.1? For example, to save Address by itself while still being in the same DC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早乙女 2024-12-02 00:28:21

据我所知,你大胆问题的答案是“否”。上下文是一个工作单元,SaveChanges 在单个事务中将每个新的、更改的或删除的对象提交到数据库。您不能选择性地说:仅保存这个或那个对象,或者仅保存状态为 Added 的实体,并且不提交状态为 ModifiedDeleted 的实体或其他什么。

作为解决方法,您可以尝试:

using (var context1 = new MyContext())
{
    Address address = context1.Addresses.Where(predicate).FirstOrDefault();
    // if address != null it is attached now to context1
    if (address == null)
    {
        // ... otherwise create new address in another context and save
        using (var context2 = new MyContext())
        {
            address = new Address { Name = name, ... }
            context2.Addresses.Add(address);
            context2.SaveChanges();
        } // context2 destroyed now and address is not attached to it anymore
        // ... and attach to context1
        context1.Addresses.Attach(address);
    }

    customer.HomeAddress = address;

    // ...

    context1.SaveChanges();
}

这样 address 永远不会同时附加到两个上下文。我不确定这是否有效。

编辑

我必须添加(因为我上面的代码看起来很奇怪)“通常”你可以单独在context1中完成所有这些操作。但我这样理解你的第 2 点,即在 SaveChanges 之前 // ... (我不明白)发生了一些事情,这会阻止你保存新的内容地址和客户同时。

The answer to your bold question is "No" as far as I can tell. The context is a unit of work and SaveChanges commits every new, changed or deleted object to the database in a single transaction. You cannot selectively say: Save only this or that object or save only entities in state Added and don't commit entities in state Modified or Deleted or something.

As a workaround you could try that:

using (var context1 = new MyContext())
{
    Address address = context1.Addresses.Where(predicate).FirstOrDefault();
    // if address != null it is attached now to context1
    if (address == null)
    {
        // ... otherwise create new address in another context and save
        using (var context2 = new MyContext())
        {
            address = new Address { Name = name, ... }
            context2.Addresses.Add(address);
            context2.SaveChanges();
        } // context2 destroyed now and address is not attached to it anymore
        // ... and attach to context1
        context1.Addresses.Attach(address);
    }

    customer.HomeAddress = address;

    // ...

    context1.SaveChanges();
}

This way address is never attached to the two contexts at the same time. I am not sure though if this works.

Edit

I must add (because my code above looks so weird) that "normally" you could do all this in context1 alone. But I understood your point 2 this way that there is something happening in // ... (which I don't understand) before SaveChanges which prevents you to save the new address and the customer at the same time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文