【发布时间】:2018-12-12 15:32:56
【问题描述】:
我有两个已经存在的表(没有外键):
客户(ID、姓名、......) 项目(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: 无效的对象名称“客户”。
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();
【问题讨论】:
标签: asp.net-core entity-framework-core fluent