C# Entityframework导航属性插入
我有这些型号的ParentID是CustomerID的外键 我想通过导航属性为客户插入地址 我该怎么办C# Entityframework导航属性插入,c#,entity-framework,C#,Entity Framework,我有这些型号的ParentID是CustomerID的外键 我想通过导航属性为客户插入地址 我该怎么办 public partial class Customer { public long CustomerID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public virtual ICollection<Address
public partial class Customer
{
public long CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Cart> Carts { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
public partial class Address
{
public long AddressID { get; set; }
public long ParentID { get; set; }
public string State { get; set; }
public string City { get; set; }
public string Address1 { get; set; }
public string PostalCode { get; set; }
public virtual Customer Customer { get; set; }
public virtual Supplier Supplier { get; set; }
}
你能再解释一下吗?!正确配置。构建对象。将对象添加到上下文中。保存更改。还有问题吗?这是一对多的关系。如果是纯SQL,您通常会如何添加?首先为“一”侧创建新记录,然后为“多”侧添加一条记录,其中包含对新创建的“一”的关键引用。按照这种模式,确保调用SaveChangesJacky Nguyen:事实上,EF从哪一方插入记录并不重要,但从多方面来看,他必须注意不要再添加两次对象。。。从关系上看,这可能是个问题。它也可以按约定进行配置。