实体框架在父母之前保存孩子。 [英] Entity Framework saving children before parent.

查看:59
本文介绍了实体框架在父母之前保存孩子。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    

我正在使用Entity Framework 6.2,代码优先。我有这两个类:

    

I am using Entity Framework 6.2, code-first. I have these two classes:

public class Agent
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.None)]
            public int? AgentId { get; set; }
            public string FirstName { get; set; }
            public string Surname { get; set; }
            public int OfficeId { get; set; }
            [ForeignKey("OfficeId")]
            public virtual Office Office { get; set; }        
        }


 





 

and

public class Office
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.None)]
            public int OfficeId { get; set; }
            public string Agency { get; set; }
            public string Branch { get; set; }
            [InverseProperty("Office")]
            public virtual ICollection<Agent> Agents { get; set; }
        }

因此每个代理都属于一个办公室(只有一个办公室)。相反,一个办公室可以包含许多代理。



我的应用程序连接到一个Web API,它通过代理和办公室提供XML数据(我使用
XDocument 为此)。然后我用这些对象填充我的 DbContext ,并调用
dbContext.SaveChanges();



此时,我收到此错误:



"无法添加或更新子行:外键约束失败(`fusion` .`agent`,CONSTRAINT`FK_agent_office_OfficeId` FOREIGN KEY (`OfficeId`)REFERENCES`office`(`OfficeId`)ON DELETE CASCADE ON  UPDATE CASCADE)"



听起来像EF试图在办公室之前保存代理商。因此,当它将第一个代理写入数据库时​​,没有相应的办公室,并且事务失败。我会想象EF会找出正确的顺序(基于导航属性的
)并以正确的顺序保存东西?
b


现在,我需要指出的是,当我的应用程序将代理和​​办公室添加到DbContext时,它首先添加了代理,然后是办公室 - 这也许就是为什么EF也以相同的顺序将它们插入到数据库中。不幸的是,当我的应用程序在Web API上调用
时,API首先向代理发送带有代理的XML数据,然后是办公室。



如何制作EF在代理商之前保存办公室?

So each agent belongs to an office (and only one office). Conversely, an office can contain many agents.

My app connects to a web API, which feeds it XML data with agents and offices (I use XDocument for this). I then populate my DbContext with these objects, and call dbContext.SaveChanges();

At that point, I get this error:

"Cannot add or update a child row: a foreign key constraint fails (`fusion`.`agent`, CONSTRAINT `FK_agent_office_OfficeId` FOREIGN KEY (`OfficeId`) REFERENCES `office` (`OfficeId`) ON DELETE CASCADE ON UPDATE CASCADE)"

It sounds to me like EF is trying to save the Agents before the Offices. So when it writes the first agent to the database, there is no corresponding office, and the transaction fails. I would imagine that EF would figure out the correct order of things (based on the navigational properties) and save things in the correct order?

Now, I need to point out that, when my app added the Agents and Offices to the DbContext, it added the Agents first, and then the Offices - and this is maybe why EF is also inserting them to the database in that same order. Unfortunately, when my app calls upon the Web API, the API sends me XML data with agents first, and then offices later.

How can I make EF save the offices before the agents?

推荐答案

请显示代码你是怎么做的?
Please show the code how are you doing ?


这篇关于实体框架在父母之前保存孩子。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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