I want to get data from two tables with single Entity class. How??
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; }
}
Above entity class holds 6 property where 3 property represents one table1 and 3 represents table2. At database table 1 holds candidate_id as primary key and table two holds candidate_id as foreign key
Update: What i did is added DBContext class
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");
});
}
}`
and at Controller Action i used following Linq to Entity Query
var data = db.homeViewModel.ToList();
But it returns nothing, i.e 0 count.