【发布时间】:2011-05-01 00:00:28
【问题描述】:
我有以下课程:
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"));
我试图确保每当我从数据库中检索到 caritem 时,产品表上都会有一个联接,这样我就可以访问产品属性,但不能反过来。
我基本上希望能够做到:
string title = cartItem.Product.Title
使用我的配置给了我一个未设置为对象异常实例的对象引用。
【问题讨论】: