gpt4 book ai didi

c# - Entity Framework 一对多插入 - 违反外键

转载 作者:太空狗 更新时间:2023-10-29 21:29:50 25 4
gpt4 key购买 nike

我是第一次使用 Entity Framework,我正在尝试创建一个带有集合的对象(并且我希望集合中的所有对象也都在数据库中创建)但是我有一些外国的按键违规。

我的示例表:

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 我有:

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

但是我收到了这个错误:{“ORA-02291:违反了完整性约束 (FK_GUESTS_APPOINTMENTS) - 未找到父键”}

我做错了什么?

更新:为了在数据库中创建 ID,我为每个表使用了一个序列,并在插入之前使用了一个触发器来获取下一个值。当我创建单个对象时,例如没有客人的约会,它会插入数据库并生成 ID。

最佳答案

这个问题的解决方法:

"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.

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

关于c# - Entity Framework 一对多插入 - 违反外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7609721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com