实体框架中的1:1关系 [英] 1 : 1 Relationship in Entity Framework

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

问题描述



我正在研究Entity Framework 4.1.我创建了一个示例应用程序,其中有三个实体
1]订单实体
订单编号
OrderDate
状态
CustomerId

2] OrderItem实体
OrderItemNo
数量
价格
订单编号

3]货运实体
ShippingId
订单编号
送货地址
电话号码


我想在订单和运送之间建立1:1关系.但是,当我在它们之间创建1:1关系时,就给我关于ShipperId的例外.
我先保存订单项,然后使用该订单号存储与该订单相关的订单项,然后保存相应订单号的运输详细信息.

您能帮我设计我的实体模型吗?

谢谢,
Umesh Tayade

Hi,

I am working on Entity Framework 4.1. I have created sample application in which i have three Entitees
1] Order Entity
OrderNumber
OrderDate
Status
CustomerId

2] OrderItem Entity
OrderItemNo
Quantity
Price
OrderNumber

3] Shipping Entity
ShippingId
OrderNumber
ShippingAddress
PhoneNumber


I want to create a 1:1 relationship between Order and shipping. But when i create the 1 : 1 relationship between them it gives me exception regarding ShipperId.
I am saving the order item first and then using that order number for storing the order items related to that order and then i am savin the shipping details for that respective order number.

Can u please help me how should i design my Entity Model.

Thanks,
Umesh Tayade

推荐答案

首先,不要只是告诉我们您收到了一个例外,如果您没有发布确切的例外,那么您也可以不这样做.完全不提.

话虽如此,EF期望两个表使用相同的主键,它们也应该使用相同的主键.
因此,将OrderNumber用作Shepping表中的主键将解决您的问题.
First of all, don''t just tell us you recieve an exception, if you do not post the exact exception then you might aswell just don''t mention it at all.

That said in a 1 to 1 relation the EF expects that the two tables use the same primary key, which they also should.
So making OrderNumber the primary key in your Shepping table would resolve your problem.


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

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