entity-framework - 首先在 Entity Framework 代码中检索关联表中的值

标签 entity-framework entity-framework-4 ado.net entity-framework-4.1

我正在使用 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 将识别 ProductIdProduct 的外键属性导航属性和 SpecificationIdSpecification 的外键属性导航属性。但是异常会提示您的 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/

相关文章:

c# - asp.net mvc项目中如何访问LocalDB

c# - Entity Framework 生成错误的 SQL

mysql - Entity Framework - 外键名称

c# - 是否可以在 SqlConnection 中使用 EF4 EntityConnection?

c# - 如何使 ServiceStack MiniProfiler 与 EF 一起使用?

.net - 为EF提供即时的自定义数据库提供程序

asp.net-mvc - 关键字不支持数据源

c# - 复杂类型引用的 EagerLoad 实体

asp.net-mvc-4 - 如何更改简单成员表的默认数据库

.net - 查看Dapper中要执行的SQL语句