我正在使用EF 4.1 code first
,我正在爲關聯實體掙扎,並獲取關聯表中設置的值。我試圖關注該帖子:Create code first, many to many, with additional fields in association table。首先在實體框架代碼中檢索關聯表中的值
我的表如下所示(所有的都是複數形式):
表:產品
Id int
Name varchar(50)
表:規格
Id int
Name varchar(50)
表:ProductSpecifications
ProductId int
SpecificationId int
SpecificationValue varchar(50)
我的相關類:
public class Product : IEntity
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ProductSpecification> ProductSpecifications { get; set; }
}
public class Specification : IEntity
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ProductSpecification> ProductSpecifications { get; set; }
}
public class ProductSpecification
{
public int ProductId { get; set; }
public virtual Product Product { get; set; }
public int SpecificationId { get; set; }
public virtual Specification Specification { get; set; }
public string SpecificationValue { get; set; }
}
我的上下文類:
public class MyContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Specification> Specifications { get; set; }
public DbSet<ProductSpecification> ProductSpecifications { get; set; }
protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
{
}
}
我的倉庫方法,我做我的電話(不知道這是否是正確的):
public class ProductRepository : IProductRepository
{
MyContext db = new MyContext();
public Product GetById(int id)
{
var product = db.Products
.Where(x => x.Id == id)
.Select(p => new
{
Product = p,
Specifications = p.ProductSpecifications.Select(s => s.Specification)
})
.SingleOrDefault();
return null; // It returns null because I don't know how to return a Product object?
}
}
這裏是錯誤,我回來了:
One or more validation errors were detected during model generation:
System.Data.Edm.EdmEntityType: : EntityType 'ProductSpecification' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �ProductSpecifications� is based on type �ProductSpecification� that has no keys defined.
這是什麼意思,沒有定義密鑰? ProductId和SpecificationId不會分別映射到產品的Id和規格的Id嗎?
我該如何返回單件產品的所有規格?