实体框架5波苏斯 - 创建和管理的关系 [英] Entity Framework 5 POCOs - creating and managing relationships

查看:114
本文介绍了实体框架5波苏斯 - 创建和管理的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF5,POCO数据库第一种方法。比方说,我需要创建一个新的客户和新的秩序: -

I'm using EF5, POCO database first approach. Let's say I need to create a new customer and a new order:-

var customer = new Customer();
var order = new Order();

order.Customer = customer;
order.CustomerId = customer.Id;

customer.Orders.Add(order);



难道我真的要包括最后三行建立的关系?在这里指出的一点是,我在内存中创建实体层级,并操纵其潜在的,直到用户点击保存很长一段时间。

Do I really have to include those last three lines to set up the relationship? The thing to point out here is that I'm creating the entity hierarchy in memory, and manipulating it for potentially a long time until the user hits "Save".

什么是困惑我是创造了一些新的客户对象的想法。每将结束为0的ID(之前被持久化)。有在我的代码不同的地方,我做订单的客户ID属性的东西。如果有多个新客户(ID为0),那么我如何才能正确的?或者我应该总是用 Order.Customer 从不 Order.CustomerId (在这种情况下,是安全的摆脱在上面的代码中的ID分配的)?

What's confusing me is the idea of creating several new Customer objects. Each will wind up with an ID of 0 (prior to being persisted). There are various places in my code where I do something with an Order's CustomerId property. If there are several new customers (ID 0) then how do I get the right one? Or should I always use Order.Customer and never Order.CustomerId (in which case, is it safe to get rid of the ID assignment in the above code)?

推荐答案

当您添加新的客户,整个图形将被插入和关系将要设置适当。你并不需要安装人工关系(见的添加一个到许多实体):

When you are adding new customer, whole graph will be inserted and relations will be setup appropriately. You don't need to setup relations manually (See Add One-to-Many Entities):

var customer = new Customer();
customer.Orders.Add(new Order());
context.Customers.Add(customer);
context.SaveChanges();

当你调用 Customers.Add 客户实体将被添加到背景和投入添加状态。同样将与所有它的相关实体(订单)发生 - 他们将添加状态。所有的图表将被插入到数据库中的SaveChanges 电话。

When you call Customers.Add customer entity will be added to context and put into Added state. Same will happen with all it's related entities (orders) - they will have Added state. All graph will be inserted to database on SaveChanges call.

请记住,这招不会为工作。更新图

Keep in mind, that this trick will not work for updating graph.

BTW:有很好的MSDN文章的定义和管理关系其中值得一读。

BTW: There is nice MSDN article Defining and Managing Relationships which worth reading.

更新:这里是当你与EF工作发生什么:

UPDATE: Here is what happen when you are working with EF:

var customer = new Customer();
var order = new Order();
customer.Orders.Add(order); // order.Customer is null



在这一点上完全EF没有参与。您可以手动设置订单对象的客户。通常我管理双向关联,添加删除方法。即为了增加订单的收集,并设置所有者这个。这取决于你在这一点上。默认情况下,为了拥有者将。下一页:

At this point EF completely not involved. You can manually set Customer of order object. Usually I manage two way associations in Add or Remove method. I.e. add order to orders collection, and set owner to this. It's up to you at this point. By default, order owner will be null. Next:

context.Customers.Add(customer); // order.Customer is customer, but no ids

下面来EF。没有什么是持久化到数据库中。 BUT EF不够了解,为了给相关客户,它将客户(业主)参考设置为这个特殊的客户智能。这不是了。但是,这样就没有什么是持久化,对象没有标识尚未

Here comes EF. Nothing is persisted to database. BUT EF smart enough to know that order relates to customer, and it will set customer (owner) reference to this particular customer. It is not null anymore. But, thus nothing is persisted, objects don't have IDs yet.

context.SaveChanges(); // ids updated



瞧。现在,英孚已经插入的对象数据库。它收到的ID和更新的对象有正确的ID。

Voila. Now EF has inserted objects to database. It received IDs, and updated objects with correct IDs.

这篇关于实体框架5波苏斯 - 创建和管理的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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