How map single entity with multiple table

前端 未结 3 855
囚心锁ツ
囚心锁ツ 2021-01-21 02:20

I want to get data from two tables with single Entity class. How??

public class HomeViewModel  
{ 
    [Key] 
    [Column(\"candidate_ID\")] 
    public int cand         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-21 02:55

    This is called entity splitting. To make it work your CandidateID must be primary key in both tables and foreign key in the second table (entity splitting works only with one-to-one relations in a database).

    Edit: There is another limitation. Entity splitting doesn't allow optional relations. Records in both tables must exists to make this work so if your table2 records are optional you must map both tables separately and construct your view model in the application from loaded records.

提交回复
热议问题