【问题标题】:Get Value from other Table by ID with Entity Framework使用实体框架通过 ID 从其他表中获取值
【发布时间】: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


    【解决方案1】:

    更新您的模型类如下:

    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

    【讨论】:

    • 无效的对象名称“客户”。客户表在另一个数据库中,然后项目是问题所在?
    • 我现在将客户表移动到与项目表相同的数据库中。现在它适用于您的代码。非常感谢!如果您知道如何使用两个数据库处理它,请告诉我(出于兴趣)谢谢
    • 更新了我的答案并添加了可能有用的链接。
    【解决方案2】:

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

    假设在数据库中的 Project 表中,“外键”是 CustomerID,将其添加到 Project 类中:

    public int CustomerID { get; set; }
    

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

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

    【讨论】:

    • 我也得到了一个 sql 异常,请参阅我的问题中的编辑。感谢您的回复
    【解决方案3】:

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

    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 查询来获取您想要的数据。

    【讨论】:

    • 嘿,谢谢您的快速回复。我尝试使用它,但 Customer Object 是 null 。 ProjectList = await _context.Project.ToListAsync();
    猜你喜欢
    • 2011-08-03
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    相关资源
    最近更新 更多