Multiple Properties for same table column

In a project that I have to maintain now are all tables mapped like this.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[Table("ab_children")]
public class Children
{
[Key]
[Column("ab_children_pk")]
public string AbChildrenPk { get; set; } = null !;
[Column("ab_address_index_fk")]
public string AbAddressIndexFk { get; set; } = null !;
}
</code>
<code>[Table("ab_children")] public class Children { [Key] [Column("ab_children_pk")] public string AbChildrenPk { get; set; } = null !; [Column("ab_address_index_fk")] public string AbAddressIndexFk { get; set; } = null !; } </code>
[Table("ab_children")]
public class Children
{    
    [Key] 
    [Column("ab_children_pk")] 
    public string AbChildrenPk { get; set; } = null !;

    [Column("ab_address_index_fk")]
    public string AbAddressIndexFk { get; set; } = null !;

}

The references to other tables are not mapped as their type but as strings which hold the foreign key value. It gets used everywhere in code. Now I want to use entity references instead wherever I change something.

I tried to add the following code to the mapping.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public AbAddress AbAddress { get; set; } = null !;
</code>
<code>public AbAddress AbAddress { get; set; } = null !; </code>
public AbAddress AbAddress { get; set; } = null !;

But that throws an error at runtime

Invalid Column Name ‘ab_cd_children_pk’

I also tried

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[Column("ab_address_index_fk")]
public AbAddress AbAddress { get; set; } = null !;
</code>
<code>[Column("ab_address_index_fk")] public AbAddress AbAddress { get; set; } = null !; </code>
[Column("ab_address_index_fk")] 
public AbAddress AbAddress { get; set; } = null !;

but I get the same exception.

Is it possible to add a second property that maps to the same table column and how?

1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[Table("ab_children")]
public class Children
{
[Key]
[Column("ab_children_pk")]
public string AbChildrenPk { get; set; } = null !;
[Column("ab_address_index_fk")]
public string AbAddressIndexFk { get; set; } = null !;
[ForeignKey(nameof(AbAddressIndexFk))]
public virtual AbAddress AbAddress { get; set; } = null!;
}
</code>
<code>[Table("ab_children")] public class Children { [Key] [Column("ab_children_pk")] public string AbChildrenPk { get; set; } = null !; [Column("ab_address_index_fk")] public string AbAddressIndexFk { get; set; } = null !; [ForeignKey(nameof(AbAddressIndexFk))] public virtual AbAddress AbAddress { get; set; } = null!; } </code>
[Table("ab_children")]
public class Children
{    
    [Key] 
    [Column("ab_children_pk")] 
    public string AbChildrenPk { get; set; } = null !;

    [Column("ab_address_index_fk")]
    public string AbAddressIndexFk { get; set; } = null !;

    [ForeignKey(nameof(AbAddressIndexFk))] 
    public virtual AbAddress AbAddress { get; set; } = null!;
}

Should be what you’re looking for. Note that the [ForeignKey] attribute will be the property name of the FK, not the database column name. If you want to use a shadow property (no FK property) then you would use the table column name and EF would create a shadow property that maps directly to that.

EF can mostly configure what you need via attributes and convention, but there will be situations it gets gunked up especially where it cannot rely on convention rules where the development team has their own distinct naming convetion. In these cases I recommend using fluent configuration either with OnModelCreating on the DbContext or via IEntityTypeConfiguration<TEntity> implementations that are registered with the DbContext model creation.

If you are getting some oddly named missing FKs etc. still, check for any explicit configuration going on with the model creation in the DbContext.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật