如何保存子实体中的EntityFramework 4? [英] How do I save a child entity in EntityFramework 4?

查看:134
本文介绍了如何保存子实体中的EntityFramework 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的订单和联系之间的1-1的关系。即Contact.OrderId引用订单,也是一个PK。

所以,我有一个现有订单,并添加新的联系人,以它像这样...

  order.Contact =新联系(){EmailAddress的=你好};
    context.Orders.Attach(订单);
    context.SaveChanges();
 

  

一个参照完整性约束冲突发生了:定义参照约束的属性值是不是本金和依赖对象的关系之间是一致的。

那我做错了吗?

解决方案

刚刚产生的子实体,设置它的 OrderIdReference 属性,你应该是好去。

I have a 1-1 relationship between Orders and Contact. i.e. Contact.OrderId references Orders and is also a PK.

So I have an existing Order and I add a new Contact to it like so...

    order.Contact = new Contact() { EmailAddress = "hello" };
    context.Orders.Attach(order);
    context.SaveChanges();

A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

So what am I doing wrong?

解决方案

Just generate your child entity, set its OrderIdReference property and you should be good to go.

这篇关于如何保存子实体中的EntityFramework 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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