[英]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.