0
我有以下類別:許多使用EF 4.1代碼第一個設計
public class CartItem
{
public long Id { get; set; }
public int Quantity { get; set; }
public Product Product { get; set; }
}
public class Product {
public long Id { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
}
目前,我有以下配置:
modelBuilder.Entity<CartItem>().HasRequired(x => x.Product).WithMany().Map(x => x.MapKey("ProductId"));
我想確保每當我檢索cartitem從數據庫中將會有一個產品表上的連接,所以我可以訪問產品屬性,但不能以其他方式訪問。
我基本上要能夠做到:使用配置我
string title = cartItem.Product.Title
讓我不設置到對象異常的實例的對象引用。