I'm not sure how to map the following tables below in EF 4.1 code first
and what objects I need representing the tables. How would I retrieve a list of the product's specifications?
I currently only have a Product
class.
Products Table:
Id
Name
IsActive
ProductSpecification Table:
ProductId
SpecificationId
Specifications Table:
Id
Name
IsActive
ProductSpecifications
is an association table. I also have the following defined in my context class:
public DbSet<Product> Products { get; set; }
EDIT
Please see my updated original post. I changed the Id of the Products and Specifications tables.
In my context class I have the following:
public DbSet<Product> Products { get; set; }
public DbSet<Specification> Specifications { get; set; }
In my repository I have the following:
public Product GetById(int id)
{
return db.Products
.Include("Specifications")
.SingleOrDefault(x => x.Id == id);
}
My Product class
(partial):
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; }
}
This was all that I did from Slauma's answer. I did not do the mapping manually as what he said I should, but I first need to understand the following:
Given my classes and tables from above, what exactly does the EF 4.1 naming conventions state on how it handles association tables? The reason why I ask is because I get the following error in my GetById method:
Invalid object name 'dbo.SpecificationProducts'.
EDIT 2
I forgot to mention the following :) A product can have a specification height as value. And for this height I need to specify a value. Like 100 inches. So I modified the ProductSpecifications table to have a value column called SpecificationValue
, this column will contain the value 100 inches. How would I modify the code to retrieve this value as well? I need to display it on my view.