实体框架代码首先一对一关系没有设置FK

时间:2013-12-11 19:31:38

标签: entity-framework ef-code-first entity-framework-6

使用EF 6.0.1

我有两个实体;购物车和购物者(简化为插图)定义为:

public class Cart
{
     public int Id { get; set; } // PK
     public int ShopperId { get; set; }
     // Navigation Property
     public virtual Shopper Shopper { get; set; }
}

public class Shopper
{
     public int Id { get; set; } // PK
     public int CartId { get; set; }
     // Navigation Property
     public virtual Cart Cart { get; set; }
}

以及以下映射

CartMap:

this.HasKey(t => t.Id);
this.HasRequired(t => t.Shopper).WithRequiredPrincipal(t => t.Cart);

和ShopperMap

this.HasKey(t => t.Id);
this.HasRequired(t => t.Cart).WithRequiredDependent(t => t.Shopper);

种子代码

var cart = new Cart{ Shopper = new Shopper()};
context.Carts.Add(cart);
context.SaveChanges();

当我尝试通过构建新购物车和新购物者来播种数据库时,新购物车和购物者会被保留,但购物车数据不会使用新购物者PK值更新,并且购物者不会更新新的购物车ID PK值。

我错过了什么?

0 个答案:

没有答案
相关问题