如何映射多台单一的实体多台、实体

由网友(钟爱一生)分享简介:我想从两个表单一的实体类中获取数据。如何?? I want to get data from two tables with single Entity class. How??public class HomeViewModel { [Key] [Column("candidate_ID")] public...

我想从两个表单一的实体类中获取数据。如何??

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; } 
}

以上实体类持有6财产,其中3财产再presents 1表1和3重presents表2。 在数据库中的表1持有candidate_id作为主键,表中的两个拥有candidate_id为外键

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

更新:我所做的是增加的DbContext类

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");
        });
     }
}`

,并在控制器动作,我用下面的LINQ到实体查询

and at Controller Action i used following Linq to Entity Query

var data = db.homeViewModel.ToList();

但它没有返回值,即0计数。

But it returns nothing, i.e 0 count.

推荐答案

检查daatbase表中的数据第一。 这是因为你可能没有表2中的相关数据。如表1的主键值(这是candidate_id)不是present表2中的外键candidateID ...

Check data in daatbase table first. It is because you might not have related data in table2. i.e table1 primary key value (which is candidate_id) is not present in table2 foreign key candidateID...

阅读全文

相关推荐

最新文章