在构建将object model
a 映射classes
到数据库表时,如何指定与其他实体的关系?
例子 :
Category
----------------------
CategoryId INT PK
Name VARCHAR
Description VARCHAR
Product
----------------------
ProductId INT PK
CategoryId INT FK
Name VARCHAR
UnitPrice DECIMAL
UnitInStock INT
class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public List<Product> Products { get; set; } //for navigation only
}
class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public decimal UnitPrice { get; set; }
public int UnitInStock { get; set; }
}
定义与其他实体的关系的最佳方式是什么?
class Product
{
//...
public Category Category;
}
或者
class Product
{
//...
public int CategoryId;
}
或两者 ?(一个用于数据库绑定(int CategoryId
)一个用于导航(Category
Category
)
class Product
{
//...
public int CategoryId;
public Category Category;
}