在構建將classes
映射到數據庫表的object model
時,如何指定與其他實體的關係?在對象模型中定義關係的最佳方法?
例子:
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;
}
您是否使用了一些類似於實體框架的ORM?如果不是,我建議你這樣做。 – mathk