Entity Framework appears to be saving children objects before the parent object

.net c# dbcontext entity-framework entity-framework-6

Question

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

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?

1
0
2/15/2017 8:31:02 AM

Accepted Answer

EF does work out the correct order based on the navigation properties. Something else is wrong. A few things might be the issue.

Why are you using [DatabaseGenerated(DatabaseGeneratedOption.None)]? Are you creating and maintaining the ids or is the database? If its the database don't you want [DatabaseGenerated(DatabaseGeneratedOption.Identity)]?

Also a nullable primary key on the agent? Is that correct? Primary keys can't/shouldn't be nullable...

You shouldn't need to set [InverseProperty("Office")] for a normal 1 to many relationship. This might be confusing EF.

1
2/16/2017 4:28:38 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow