简体   繁体   English

实体框架一对多插入 - 外键违规

[英]Entity Framework One-To-Many Insert - Foreign Key violation

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. 我第一次使用Entity Framework,我正在尝试使用集合创建一个对象(我希望集合中的所有对象也可以在数据库中创建)但是我有一些外键违规。

My sample tables: 我的样本表:

table APPOINTMENTS: ID, VAR1, DATE_APPOINTMENT
table GUESTS: ID, APPOINTMENT_ID, USER_ID, VAR2, VAR3

My test code: 我的测试代码:

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);

At DomainService I have: 在DomainService,我有:

public void InsertAppointment(APPOINTMENTS appointment)
{
    using (var context = this.ObjectContext)
    {
        context.AddToAPPOINTMENTS(appointment);
        context.SaveChanges();
    }
}

But I'm getting this error: {"ORA-02291: integrity constraint (FK_GUESTS_APPOINTMENTS) violated - parent key not found"} 但我收到此错误:{“ORA-02291:违反完整性约束(FK_GUESTS_APPOINTMENTS) - 未找到父密钥”}

What am I doing wrong? 我究竟做错了什么?

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. 更新:要在数据库中创建ID,我在插入前使用每个表的序列和触发器来获取下一个值。 When I create a single object, eg a appointment without guests, it inserts in the database and it generates the id. 当我创建单个对象时,例如没有来宾的约会,它会插入数据库并生成id。

The solution to this problem: 解决这个问题的方法:

"The ID fields that are generated from sequences won't be handled correctly. After saving the entities the ID's will be returned as 0. I'll fix this by manually hacking the SSDL (open your .edmx file in a text editor) with StoreGeneratedPattern="Identity" attributes on the ID fields (lines 6 and 16). Note that designer may rip that change out upon future modification. “从序列生成的ID字段将无法正确处理。保存实体后,ID将返回为0.我将通过手动破解SSDL(在文本编辑器中打开.edmx文件)来修复此问题。 StoreGeneratedPattern = ID字段上的“Identity”属性(第6行和第16行)。请注意,设计人员可能会在将来修改时删除该更改。

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. 虽然我认为这并非绝对必要,但修改某些类型元数据也可能是谨慎的,例如将SSDL中的“number”更改为“int”,并在适用的CSDL中将“Decimal”更改为“Int32”。 Frequently these don't auto-generate with the desired values especially with XE." 通常这些不会自动生成所需的值,尤其是XE。“

@http://www.chrisumbel.com/article/oracle_entity_framework_ef @http://www.chrisumbel.com/article/oracle_entity_framework_ef

As for me, the problem was solved simply by opening diagram .edmx and changing property StoreGeneratedPattern from None to Identity for each Primary Key in each table. 至于我,只需打开图表.edmx并将每个表中每个主键的属性StoreGeneratedPattern从None更改为Identity,就可以解决问题。 After saving everything was fine. 保存后一切都很好。

I'm using VS 2012, Entity Framework 5 (6 is not supported yet), Oracle 11.2, last ODP.NET 12, .Net 4.5 我正在使用VS 2012,Entity Framework 5(尚不支持6),Oracle 11.2,最后一个ODP.NET 12,.Net 4.5

In case of EF code first approach, if this error come 如果是EF代码第一种方法,如果出现此错误

(ORA-02291: integrity constraint (FK_GUESTS_APPOINTMENTS) violated - parent key not found) (ORA-02291:违反了完整性约束(FK_GUESTS_APPOINTMENTS) - 未找到父密钥)

In my case there are 2 tables which have Identity columns. 在我的例子中,有2个表具有标识列。 So I just added 所以我刚才补充道

[DatabaseGenerated(DatabaseGeneratedOption.Identity)] 

property to my model class just above the the column which is identity column in database and it solved my problem :) 我的模型类的属性就在数据库中的标识列的列上方,它解决了我的问题:)

Hope this help :) 希望这有帮助:)

I cant see where you are setting your Primary Key (the ID property of the appointment class). 我无法确定您在哪里设置主键(约会类的ID属性)。 Are you using a key generator on the database side? 你在数据库端使用密钥生成器吗? If not this should be the problem. 如果不是这应该是问题。

您正在插入一个记录,该记录具有在约束引用的父表中找不到的外键值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM