我正在做一些测试来体验使用 LINQ to SQL 的对象关系映射,但我一直在尝试使多对多关系工作。为了简单起见,假设我有一个Order类,它拥有一个有序Products的集合。如何使用 LINQ to SQL 映射此产品集合?
[Table(Name = "OrderTable")]
public class Order
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int ID;
[Column]
public string Customer;
[Association(ThisKey = "ID", OtherKey = "ID")] // Not working
public EntitySet<Product> products = new EntitySet<Product>();
}
[Table(Name = "ProductTable")]
public class Product
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int ID;
[Column]
public string Name;
[Column]
public double Price;
}
我尝试使用EntitySet作为容器来保存Order的Products但它不起作用,因为 LINQ 查询返回带有空产品集合的Order对象。
PS:从这个问题看来,我需要创建第三个类来跟踪订单-产品关系,但我希望找到一种无需创建另一个类的方法。