Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core: How to add a composite object?

There is the following composite object, for example:

public class Parent
{
    [Key]
    public int Id { get; set; }

    [ForeignKey("ChildRefId")]
    public Guid ChildId { get; set; }

    public Child Child { get; set; }
}

public class Child
{
    [Key]
    public Guid Id { get; set; }

    [ForeignKey("ParentRefId")]
    public int ParentId { get; set; }

    public Parent Parent { get; set; }
}

Parent and Child has one-to-one relation:

modelBuilder.Entity<Parent>()
    .HasOne(parent => parent.Child)
    .WithOne(child => child.Parent)
    .WillCascadeOnDelete();

I try to create new Parent with Child and save it in the DB:

var parent = new Parent { Child = new Child() };
dbContext.Parents.Add(parent);
dbContext.SaveChanges(); //<-- Exception!

...and get the following exception:

System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Parent_Child_ChildId". The conflict occurred in database "MyDatabase", table "dbo.Child", column 'Id'.

If I use the db context to create a child it works fine:

var child = dbContext.Childs.Add(new Child());
var parent = new Parent { Child = child };
dbContext.Parents.Add(parent);
dbContext.SaveChanges(); //<-- It works fine

Question #1: I can easily add new entities to a nested collections without using context for create it. But it not works for one-to-one relation. For example:

var parent = new Parent();
parent.Grandparents.Add(new Grandparent();
dbContext.Parents.Add(parent);
dbContext.SaveChanges(); //<-- It works fine and parent with one new grandparent were created!

Why?

Question #2: Please, describe me what I do wrong and how I can save new parent with a child without context to create a child (like nested collections)?

Thank you!

like image 486
hcp Avatar asked Feb 25 '26 00:02

hcp


1 Answers

This error is occurring only for the beta-8 version of EF. After I was updated EF to the rc1-final version error disappeared. It may be explained there is no graph tracking on the beta-8 version yet.

You can find more detailed information here.

like image 119
hcp Avatar answered Feb 27 '26 14:02

hcp