我有两个数据库表-Customer_Values和Customer_Attributes ...,这很愚蠢。糟透了。
Customer_Values具有类似的东西
CustomerId | AttributeId | Value
--------------------------------
01 | 01 | Steve
02 | 01 | Frank
01 | 02 | Smith
etc...
Customer_Attributes看起来像这样
AttributeId | Value
------------------------
01 | First Name
02 | Last Name
我想将第一个表映射到这样的Entity对象中...
class {
string FirstName { get; set; }
string LastName { get; set; }
}
但是我完全不确定该怎么做。
我使用的是EF 6,代码优先
答案 0 :(得分:0)
假设您的上下文如下(请注意FK参考,我们可以设置模型以通过Customer_Values启用FK的参考):
class MyContext : DbContext
{
public DbSet<Customer_Values> Customer_Value { get; set; }
public DbSet<Customer_Attributes> Customer_Attribute { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer_Attributes>()
.HasOne(v => v.Customer_Values)
.WithOne(a => a.Customer_Attributes)
.HasForeignKey(v => v.AttributeId)
.HasConstraintName("FK_Constraint_Name_Here");
}
}
客户值参考客户属性:
public class Customer_Values
{
public int CustomerId { get; set; }
public int AttributeId { get; set; }
public string Value { get; set; }
public <Customer_Attributes> Customer_Attribute { get; set; }
}
客户属性通过1:1关系获取客户ID
public class Customer_Attributes
{
public int AttributeId { get; set; }
public string Value { get; set; }
public int CustomerId { get; set; }
}
希望这给您正确的想法!