Answer the question
In order to leave comments, you need to log in
How to correctly work with child-parent relationships in Entity Framework 6?
there is Parent and Child one-to-one
public class Parent
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int? Id { get; set; }
public Child Child { get; set; }
}
public class Child
{
[Key, ForeignKey("Parent")]
public int ParentId { get; set; }
public virtual Parent Parent{ get; set; }
}
var parent = new Parent();
var child = new Child();
parent.Child = child;
// child.Parent = parent; // Если раскомментировать, будет ошибка: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values
using (var appDbContext = new AppDbContext()) {
appDbContext .Parents.AddOrUpdate(parent);
appDbContext .SaveChanges();
}
child.Parent
in code. child.Parent
. It seems logically it seems that this is how it should be, but I don’t want to rewrite a lot :)Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question