我希望你能用单个实体类从两个表中获取数据。怎么做呢?
public class HomeViewModel
{
[Key]
[Column("candidate_ID")]
public int candidateID { get; set; }
[Column("first_name")]
public string firstName { get; set; }
[Column("last_name")]
public string lastName { get; set; }
public string emailID { get; set; }
public string mb_country_code { get; set; }
public int mobile_no { get; set; }
}
以上实体类包含6个属性,其中3个属性表示table1,另外3个属性表示table2。 在数据库中,table1拥有candidate_id作为主键,而table2则将candidate_id作为外键。
更新:我添加了DBContext类。
public class EmployeeMonitoring : DbContext
{
public DbSet<HomeViewModel> homeViewModel { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<HomeViewModel>().Map(m =>
{
m.Properties(a => new { a.candidateID, a.firstName, a.lastName,a.status });
m.ToTable("table1");
}).Map(m =>
{
m.Properties(c => new { c.candidateID,c.emailID, c.mobile_no, c.mb_country_code });
m.ToTable("table2");
});
}
}`
在控制器操作中,我使用以下Linq到Entity查询:
var data = db.homeViewModel.ToList();
但它没有返回任何结果,即数量为0。