问题描述
我使用的实体框架是第一次,我想创建一个集合的对象(我想在数据库中创建以及集合中的所有对象),但我有一些外国。键违反
I'm using Entity Framework for the first time and I'm trying to create a object with a collection (and I want all the objects in the collection to be created in database as well) but I'm having some foreign keys violations.
我的示例表:
table APPOINTMENTS: ID, VAR1, DATE_APPOINTMENT
table GUESTS: ID, APPOINTMENT_ID, USER_ID, VAR2, VAR3
我的测试代码:
DomainService aux = new DomainService();
APPOINTMENTS appointment = new APPOINTMENTS();
appointment.VAR1 = "BLA";
appointment.DATE_APPOINTMENT = new DateTime();
//The user with id = 1 is already created in the database
appointment.GUESTS.Add(new GUESTS { USER_ID = 1, VAR2 = 1, VAR3 = "F" });
aux.InsertAppointment(appointment);
目前的DomainService我有:
At DomainService I have:
public void InsertAppointment(APPOINTMENTS appointment)
{
using (var context = this.ObjectContext)
{
context.AddToAPPOINTMENTS(appointment);
context.SaveChanges();
}
}
但我发现了这个错误:
{ORA-02291:诚信约束(FK_GUESTS_APPOINTMENTS)侵犯 - 没有找到父键}
But I'm getting this error:{"ORA-02291: integrity constraint (FK_GUESTS_APPOINTMENTS) violated - parent key not found"}
我在做什么错了?
更新:
。要创建在数据库中的ID,我使用的每个表中序列和插入前触发器得到下一个值。
当我创建一个对象,例如。没有客人预约,它插入到数据库和它生成的ID
UPDATE:To create the ID's in the database, I am using a sequence for each table and a trigger before insert to get the next value.When I create a single object, e.g. a appointment without guests, it inserts in the database and it generates the id.
推荐答案
解决这个问题:
这是从序列生成的ID字段将不能正确处理
,保存的ID将返回为0的实体后
我会用StoreGeneratedPattern =身份手动黑客SSDL(打开一个
文本编辑器中.edmx文件),解决这个问题的
ID字段属性(线6和16 )。请注意,设计师可以撕裂在未来的修正版,改变了
虽然我想这是不是绝对必要的,也可能是审慎
到修改一些类型的元数据,如改变数字s到INTS在
你SSDL和十进制s到的Int32在你的CSDL适用
通常这些不自动与所需的值
尤其是XE生成
While I suppose it's not absolutely necessary it might also be prudent to modify some type metadata such as changing "number"s to "int"s in your SSDL and "Decimal"s to "Int32"s in your CSDL where applicable. Frequently these don't auto-generate with the desired values especially with XE."
@http://www.chrisumbel.com/article/ oracle_entity_framework_ef
@http://www.chrisumbel.com/article/oracle_entity_framework_ef
这篇关于Entity Framework的一个一对多插入 - 外键冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!