I'm not sure how to map the following tables below in EF 4.1 code first and what objects I need to represent the tables. How to get a list of product specifications?
Currently, I only have a Product class.
Products Table: Id Name IsActive ProductSpecification Table: ProductId SpecificationId Specifications Table: Id Name IsActive
ProductSpecifications - table of associations. I also have the following definition in my context class:
public DbSet<Product> Products { get; set; }
EDIT
See my updated original post. I have changed the tables of product identifiers and specifications.
In my class 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; } }
That is all I have made from the answer of Slauma. I didn’t do the mapping manually, as he said I should, but I need to understand the following first:
Given my classes and tables above, what exactly do EF 4.1 naming conventions conclude in how they handle association tables? The reason I'm asking is because I get the following error in the GetById method:
Invalid object name 'dbo.SpecificationProducts'.
EDIT 2
I forgot to mention the following: a product may have a specification height as a value. And for this height I need to specify a value. Like 100 inches. So I changed the ProductSpecifications table to a column of SpecificationValue values, this column will contain a value of 100 inches. How do I change the code to get this value? I need to display it in my view.