Foreign key with constant in Fluent API

非 Y 不嫁゛ 提交于 2020-01-02 06:17:33

问题


I need to load a reqired:many relationship from database. Now my problem is, that the the key of the related table consists of three keys:

public partial class EnumValue
{
    [Key]
    [Column(Order = 0)]
    [StringLength(14)]
    public string EnumGroup { get; set; }

    [Key]
    [Column(Order = 1)]
    public byte EnumId { get; set; }

    [Key]
    [Column(Order = 2)]
    [StringLength(3)]
    public string Language { get; set; }

    [StringLength(50)]
    public string Description { get; set; }

}

In my other object I have only one property to fill the foreign key, the other parts are constants specific for this object.
I tried to build the relations in EF6, but can't get it to work with model builder and the Fluent API using constants instead of properties:

modelBuilder.Entity<SupplierCondition>()                
    .HasRequired(t => t.ConditionTypeLookupRef)
    .WithMany()
    .HasForeignKey(t => new { "PArt", t.ConditionType, "EN" });

How can I pass constants as a value for a foreign key in Fluent API?


回答1:


You can't use there const values because there isn't assign any value, you inform fluent API, which property refers to another table, when you pass "text", or 2, or new List(), it won't work they aren't properties, i think you can divide it to three tables, and use it as tags. But I can't see the bigger picture.



来源:https://stackoverflow.com/questions/50740883/foreign-key-with-constant-in-fluent-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!