我正在使用 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)
表:产品规范
ProductId int
SpecificationId int
SpecificationValue varchar(50)
我的相关类(class):
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 吗?
我如何退回具有所有规范的单个产品?
最佳答案
Entity Framework 将识别 ProductId
是 Product
的外键属性导航属性和 SpecificationId
是 Specification
的外键属性导航属性。但是异常会提示您的 ProductSpecification
上缺少主键(“Key”=“Primary Key”)。实体。每个实体都需要定义一个关键属性。这可以通过约定发生 - 通过键属性的特定命名 - 或明确地使用数据注释或 Fluent API。
您的 ProductSpecification
类没有 EF 按照约定将其识别为键的属性:否 Id
属性(property)和没有ProductSpecificationId
(类名+“Id”)。
所以你必须明确地定义它。使用数据注释定义它显示在您链接的帖子中:
public class ProductSpecification
{
[Key, Column(Order = 0)]
public int ProductId { get; set; }
public virtual Product Product { get; set; }
[Key, Column(Order = 1)]
public int SpecificationId { get; set; }
public virtual Specification Specification { get; set; }
public string SpecificationValue { get; set; }
}
在 Fluent API 中,它将是:
modelBuilder.Entity<ProductSpecification>()
.HasKey(ps => new { ps.ProductId, ps.SpecificationId });
两种方式都定义了一个复合键,每个部分都是
Product
的外键。或 Specification
同时表。 (您不需要明确定义 FK 属性,因为 EF 因其约定友好的名称而识别它。)您可以退回包含所有规范的产品,并使用预先加载,例如:
var product = db.Products
.Include(p => p.ProductSpecifications.Select(ps => ps.Specification))
.SingleOrDefault(x => x.Id == id);
关于entity-framework - 首先在 Entity Framework 代码中检索关联表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8007058/