我不确定如何映射下面的表格EF 4.1 code first
以及我需要哪些对象来代表表格。我如何检索产品规格列表?
我目前只有一个Product
class.
Products Table:
Id
Name
IsActive
ProductSpecification Table:
ProductId
SpecificationId
Specifications Table:
Id
Name
IsActive
ProductSpecifications
是一个关联表。我还在上下文类中定义了以下内容:
public DbSet<Product> Products { get; set; }
EDIT
请参阅我更新的原始帖子。我更改了产品和规格表的 ID。
在我的上下文类中,我有以下内容:
public DbSet<Product> Products { get; set; }
public DbSet<Specification> Specifications { get; set; }
在我的存储库中,我有以下内容:
public Product GetById(int id)
{
return db.Products
.Include("Specifications")
.SingleOrDefault(x => x.Id == id);
}
My Product class
(部分的):
public class Product : IEntity
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public ICollection<Specification> Specifications { get; set; }
}
My Specification class
:
public class Specification : IEntity
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public ICollection<Product> Products { get; set; }
}
这就是我从 Slauma 的回答中所做的一切。我没有像他说的那样手动进行映射,但我首先需要了解以下内容:
鉴于上面的类和表,EF 4.1 命名约定对于如何处理关联表究竟有何规定?我之所以问这个问题是因为我在 GetById 方法中收到以下错误:
Invalid object name 'dbo.SpecificationProducts'.
EDIT 2
我忘记提及以下内容:) 产品可以将规格高度作为值。对于这个高度,我需要指定一个值。比如100英寸。因此,我修改了 ProductSpecifications 表,使其具有一个名为的值列SpecificationValue
,此列将包含值 100 英寸。我将如何修改代码来检索该值?我需要将其显示在我的视图中。