通过实体框架通过ID从其他表中获取价值

时间:2018-07-04 08:47:49

标签: asp.net-core entity-framework-core fluent

我有两个已经存在的表(没有外键):

客户(ID,名称,.....) 项目(编号,客户编号,名称)

在我的asp.net核心应用程序中,我有两个模型:

public class Customer {
   public int Id { get; set; };
   public String Name { get; set; };
}

public class Project {
   public int Id { get; set; };
   public Customer Customer{ get; set; };
   public String Name{ get; set; };
}

以及与此相关的datacontext类

public class CustomerContext: DbContext
{
    public CustomerContext(DbContextOptions<CustomerContext> options) : base(options)
    {
    }

    public DbSet<CustomerContext> Customer { get; set; }
}

public class ProjectContext: DbContext
{
    public ProjectContext(DbContextOptions<ProjectContext> options) : base(options)
    {
    }

    public DbSet<ProjectContext> Project{ get; set; }
}

但是我无法找到如何通过customerId在Projectclass中获取Customer对象

有人可以帮我吗?谢谢

编辑:现在我像下面的答案中那样更改模型类

但是在加载页面时出现以下错误 SqlException:无效的对象名称“客户”。

        projectList = await (from project in _context.Project
                                     join customer in _customerContext.Customer on project.CustomerId equals customer.Id into tmp
                                     from m in tmp.DefaultIfEmpty()

                                     select new Project
                                     {
                                         Id = sollIst.Id,
                                         CustomerId = sollIst.CustomerId,
                                         Customer = m,
                                         Name = sollIst.Name,
                                     }
                      ).ToListAsync();

3 个答案:

答案 0 :(得分:3)

如下更新模型类:

public class Customer {
   public int Id { get; set; };
   public String Name { get; set; };
}

public class Project {
   public int Id { get; set; };
   public String Name{ get; set; };
   public int CustomerID { get; set; }
   [ForeignKey("CustomerID")]
   public Customer Customer{ get; set; };
}

将两个DbContext合并为一个。

public class ProjectContext: DbContext
{
    public ProjectContext(DbContextOptions<ProjectContext> options) : base(options)
    {
    }

    public DbSet<Project> Projects { get; set; }
    public DbSet<Customer> Customers { get; set; }
}

然后执行

projectList = await (from project in _context.Project
                 join customer in _context.Customer on project.CustomerId equals customer.Id into tmp
                 from m in tmp.DefaultIfEmpty()

                 select new Project
                 {
                     Id = sollIst.Id,
                     CustomerId = sollIst.CustomerId,
                     Customer = m,
                     Name = sollIst.Name,
                 }
  ).ToListAsync();

我希望以下链接将帮助您了解如何在不同数据库中联接两个表。

  1. Joining tables from two databases using entity framework.
  2. Entity framework join across two databases

答案 1 :(得分:2)

您将必须在Project类中创建一个表示“外键”的属性。

让我们在数据库的Project表中说“外键”是CustomerID,将其添加到Project类中:

range(number + 1)

然后将ForeignKey属性添加到Customer属性:

public int CustomerID { get; set; }

答案 2 :(得分:0)

首先,您的模型类应如下所示:

CREATE TABLE dbo.TestTable (ID int, String varchar(100) NOT NULL DEFAULT 'test')
GO
--INSERT is successful, String has a value of 'test'
INSERT INTO dbo.TestTable (ID)
VALUES(1);
GO
--INSERT fails, String cannot have a value of NULL
INSERT INTO dbo.TestTable (ID,
                           String)
VALUES(2,NULL);
GO
SELECT *
FROM dbo.TestTable;
GO
DROP TABLE dbo.TestTable;
GO

然后您的DbContext类应该如下:

public class Customer {
   public int Id { get; set; };

   public string Name { get; set; };
}

public class Project {
   public int Id { get; set; };

   [ForeignKey("Customer")]
   public int CustomerId{ get; set; };

   public string Name{ get; set; };

   public Customer Customer { get; set; };
}

现在,您可以使用Entity Framework Core和LINQ查询来获取所需的数据。