简体   繁体   English

如何使用实体框架将属性映射到不同的表

[英]How to map a property to a different table using Entity Framework

My database looks something like the following:我的数据库如下所示:

dbo.Products Table: dbo.Products 表:

Id        | int
Status    | tinyint

dbo.ProductNames Table: dbo.ProductNames 表:

ProductId | int
Name      | nvarchar(4000)

And a simplifed version of the code would be:代码的简化版本是:

public class Product{
    public int Id { get; set; }
    public string Name { get; set; }
    public ObjectStatus Status { get; set; }
}

public class ProductEntityConfiguration : EntityConfiguration<Product> {
    public ProductEntityConfiguration() {
        ToTable("Products");
        HasKey(p => p.Id);
        Property(p => p.Id).HasColumnName("Id");
        Property(p => p.Status).HasColumnName("Status");
    }
}

How can I map the product name, which exists in a different table as a normal property, so that if someone requests it, EF must join the two tables by the Id?如何映射作为普通属性存在于不同表中的产品名称,以便如果有人请求它,EF 必须通过 Id 连接两个表?

I think you have to use entity splitting in this case.我认为在这种情况下您必须使用实体拆分。 You have one entity (Product) and you want to split it into two tables (Products and ProductNames).您有一个实体 (Product),并且希望将其拆分为两个表(Products 和 ProductNames)。 Try to use the following mapping:尝试使用以下映射:

  public class ProductEntityConfiguration : EntityConfiguration<Product> 
  {
      public ProductEntityConfiguration() 
      {
         Map(m => 
         { 
            m.Property(t => t.ProductId).HasColumnName("MyIdChanged");
            m.Property(t => t.Status).HasColumnName("Status");                
            m.ToTable("Product")
         }) 
         .Map(m => 
         { 
            m.Property(t => t.ProductId).HasColumnName("MyProductIdChanged");
            m.Property(t => t.Name).HasColumnName("MyProductName");
            m.ToTable("ProductNames"); 
         });
      }
  }

This will configure the appropriate primary keys and foreign keys and do the joins automatically.这将配置适当的主键和外键并自动执行连接。

I know this is super late, but I was researching the same issue and I find the accepted answer is a little too complicated to my liking (I need to join 3 tables), and I prefer my code to be simpler.我知道这太晚了,但我正在研究同样的问题,我发现接受的答案对我来说有点太复杂了(我需要加入 3 个表),我更喜欢我的代码更简单。

So I chose to use [NotMapped] property instead, which makes the code simpler.所以我选择使用[NotMapped]属性来代替,这使得代码更简单。

Here is a sample solution based on the question above.这是基于上述问题的示例解决方案。

public class Product{
    public int Id { get; set; }    
    public ObjectStatus Status { get; set; }

    // Foreign keys
    public int ProductNameId { get; set; }
    public virtual ProductName ProductName { get; set; }

    [NotMapped]
    public string Name => ProductName?.Name;    
}

public class ProductEntityConfiguration : EntityConfiguration<Product> {
    public ProductEntityConfiguration() {
        ToTable("Products");
        HasKey(p => p.Id);
        Property(p => p.Id).HasColumnName("Id");
        Property(p => p.Status).HasColumnName("Status");

        Property(p => p.ProductNameId).HasColumnName(@"ProductNameId").HasColumnType("int").IsRequired();
        HasRequired(a => a.ProductName).WithMany(b => b.Products).HasForeignKey(c => c.ProductNameId);
    }
}

This solution may 'look' complicated than the accepted answer because I am adding a foreign key here.这个解决方案可能比接受的答案“看起来”复杂,因为我在这里添加了一个外键。 But I bet most people already have these foreign keys anyway, so all you need to do is add the NotMapped property and you are done.但我敢打赌,无论如何大多数人都已经拥有这些外键,因此您只需添加NotMapped属性即可。 It would be just a 1 line change.这将只是 1 行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用SQL映射具有列表属性的实体框架模型 - How to map an Entity Framework model that has a list property using SQL 如何在Entity Framework 4中映射枚举属性 - How to map an enum property in Entity Framework 4 首先在不同架构中映射具有相同表的实体 Entity Framework 6 代码 - Map entity with same table in different schemas Entity Framework 6 code first 实体框架:如何将实体的属性映射到存储的proc? - Entity Framework: How to map an entity's property to a stored proc? Entity Framework Core:如何将不同的对象实例映射到同一个实体? - Entity Framework Core: how to map different object instances to the same entity? 实体框架:如何将具有不同键的多个表映射到一个实体? - Entity Framework: how to map multiple tables with different keys to one entity? 如何在实体框架-6中的onmodelcreating方法中映射不同于模型的表名和列名? - How to map table names and column name different from model in onmodelcreating method in Entity Framework -6? 如何在Entity Framework 7中将实体映射到表? - How do you do map an entity to an table in Entity Framework 7? Map 使用 Entity Framework Core 的图像类型属性 - Map Image type property using Entity Framework Core 如何在实体框架核心中将属性映射到类实例? - How can I map a property to a class instance in entity framework core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM