通过 ID Entity Framework 从其他 Table 获取价值

Get Value from other Table by ID with Entity Framework

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

客户(身份证、姓名、.....) 项目(Id、CustomerId、名称)

在我的 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; };
}

以及此

的数据上下文 类
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 对象

有人可以帮助我吗?谢谢

编辑:现在我改变我的模型 类 就像下面的答案

但是在加载页面时我得到了一个 SQL 异常 SqlException: 无效的对象名称 'Customer'.

        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();

首先,你的模型类应该如下:

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

那么你的 DbContext 类 应该如下所示:

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

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

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

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

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

您必须在项目 class 中创建一个 属性 来代表 "foreign key"。

假设在数据库中的项目 table 中,"foreign key" 是 CustomerID,将其添加到项目 class:

public int CustomerID { get; set; }

然后给Customer添加ForeignKey属性属性:

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

更新您的模型 类 如下:

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.