简体   繁体   中英

Entity framework 6 code first one to one instead of one to many

I got over a project where EF6 code first is used. I have 2 classes where a relationship one to many is set, the same is set in Fluent API.

When I create both parent/child in the same step everything is fine and saved, but when I create the parent first, save it and then I want to add a child I got an exception that states that these two entities are in relationship 1 to 0..1 and it is not saved.

Is there any way to first create only the parent and then add a child to it?

Model classes:

public class Parent
{
    public int ParentId {get; set;}
    public virtual ICollection<Child> Children {get; set;}
}

public class Child
{
    public int ChildId {get; set;}
    public virtual Parent Parent {get; set;}
}

Fluent API:

modelBuilder.Entity<Child>()
                     .HasRequired<Parent>(s => s.Parent)
                     .WithMany(s => s.Children)
                     .HasForeignKey(s => s.ParentId);

In database I see the foreign key ParentId . Parent can exist without a child if I am right. Everything is saved with dbContext.SaveChanges()

Exception:

Exception thrown: 'System.Exception' in Project.dll

Additional information: Multiplicity constraint violated. The role 'Child_Parent_Target' of the relationship 'Project.DAL.Child_Parent' has multiplicity 1 or 0..1.

Consider giving your child a ParentId Property

public class Child
{
    public int ChildId {get; set;}

    public int ParentId {get; set;}
    public virtual Parent Parent {get; set;}
}

If you do this, you follow the code-first conventions (click to view) . If you follow the conventions Entity Framework understands that Parent - Child is a one-to-many relation: A parent has zero or more childs and a child has exactly one parent.

The nice thing about following the conventions is that you don't need your fluent API statements. Entity Framework knows that the parent is required, that the parent has many children and that the foreign key to the parent is ParentId

Back to your problem. After adding the ParentId to your child, you can add a parent without children and add children later.

After adding ParentId you can add a parent and add children later

public class Parent
{
    public int ParentId {get; set;}
    public string Name {get; set;}

    public virtual ICollection<Child> Children {get; set;}
}

public class Child
{
    public int ChildId {get; set;}
    public string Name {get; set;}

    // By convention will become the foreign key to Parent:
    public int ParentId {get; set;}
    public virtual Parent Parent {get; set;}
}

public class MyDbContext : DbContext
{
    public DbSet<Parent> Parents {get; set;}
    public DbSet<Child> Children {get; set;}
}

public void Main()
{
    using (var dbContext = new MyDbContext(...))
    {
        var addedParent = dbContext.Parents.Add(new Parent()
        {
             Name = "Phil Dunphy",
        }

        // if you do not want to add a child now, you can SaveChanges
        dbContext.SaveChanges();

        // now addedParent has a ParentId, you can add a child:
        var addedChild = dbContext.Children.Add(new Child()
        {
            Name = "Luke Dunphy",
            ParentId = addedParent.Id,
        };
        dbContext.SaveChanges();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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