知其然不知其所以然的悲惨后果【EF CodeFirst 实体关系两日游】

先上测试代码

    public class User
{
public int ID { get; set; }

public int BillingAddressID { get; set; }
public Address BillingAddress { get; set; }
public IList<Shipment> Shipments { get; set; }
}

public class Address
{
public int ID { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string ZipCode { get; set; }

}

public class Shipment
{
public int ID { get; set; }
public string State { get; set; }
public int DeliveryAddressID { get; set; }

public Address DeliveryAddress { get; set; }

public User ShipUser { get; set; }
//[ForeignKey("ShipUser")]
public int ShipUserID { get; set; }
//public int UserId { get; set; }
}

public class TestContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<Shipment> Shipments { get; set; }


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Shipment>().HasRequired(u => u.ShipUser)
.WithMany(d => d.Shipments)
.HasForeignKey(c => c.ShipUserID)
.WillCascadeOnDelete(false);
}
}

上面代码是能成功建库,并新增数据的最终版。

下面通过修改会报各种各样的错误

1、去掉OnModelCreating重载方法

  报错:

SqlException: Introducing FOREIGN KEY constraint 'FK_Shipments_Users_ShipUserID' on table 'Shipments' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.

 分析原因:EF创建的FK默认是带级联的

  

  因为SqlServer并不是真正运行去校验是否循环或多重级联,而是通过一个级联路径(可能与Name有关系)

  如果主键列不叫ID,也没有问题,但这样EF在建库时,会自动创建一列外键列(如ShipUser_UserID在代码中看不到)



posted @ 2012-03-27 17:42  湛然  阅读(5151)  评论(1编辑  收藏  举报