Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF核心从一到多到多的迁移_C#_Postgresql_Entity Framework_Entity Framework Core_Entity Framework Migrations - Fatal编程技术网

C# EF核心从一到多到多的迁移

C# EF核心从一到多到多的迁移,c#,postgresql,entity-framework,entity-framework-core,entity-framework-migrations,C#,Postgresql,Entity Framework,Entity Framework Core,Entity Framework Migrations,我有一对多关系的实体。例如,从部门到员工。在本例中,“Employee”实体(表)中有“DepartmentID”列 以下是现有实体,用于说明- public class Department { public int Id {get;set;} public string Name {get;set;} public List<Employee> Employees {get;set;} } public class Employee { p

我有一对多关系的实体。例如,从部门到员工。在本例中,“Employee”实体(表)中有“DepartmentID”列

以下是现有实体,用于说明-

public class Department 
{
    public int Id {get;set;}
    public string Name {get;set;}

    public List<Employee> Employees {get;set;}
}

public class Employee 
{
    public int Id {get;set;}
    public string Name {get;set;}

    public int DepartmentId {get;set;}
}
public class Department 
{
    public int Id {get;set;}
    public string Name {get;set;}  

    public List<DepartmentEmployee> DepartmentEmployees {get;set;}
}

public class Employee 
{
    public int Id {get;set;}
    public string Name {get;set;}

    public List<DepartmentEmployee> DepartmentEmployees {get;set;}
}

public DepartmentEmployee 
{
    public int DepartmentId {get;set;}
    public Department Department {get;set;}

    public int EmployeeId {get;set;}
    public Employee Employee {get;set;}
}
公共课部
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表雇员{get;set;}
}
公营雇员
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int DepartmentId{get;set;}
}
现在在下一个版本中,我想将这种关系转换为多对多。所以,“DepartmentID”列将从Employee表中删除,并引入新的中间实体(表)“DepartmentEmployee”来管理多对多

由于应用程序已经投入生产,我想使用EF Core中的迁移功能自动迁移它。对于上面的示例,我必须从Employee表中删除'DepartmentID'列。但是,在删除映射数据之前,应该将其迁移到新表中(即“DepartmentEmployee”)。也就是说,应该将员工ID和相应的部门ID添加到“DepartmentEmployee”表中。完成后,我可以从“Employee”表中删除“DepartmentID”列

以下是用于说明目的的新提议实体-

public class Department 
{
    public int Id {get;set;}
    public string Name {get;set;}

    public List<Employee> Employees {get;set;}
}

public class Employee 
{
    public int Id {get;set;}
    public string Name {get;set;}

    public int DepartmentId {get;set;}
}
public class Department 
{
    public int Id {get;set;}
    public string Name {get;set;}  

    public List<DepartmentEmployee> DepartmentEmployees {get;set;}
}

public class Employee 
{
    public int Id {get;set;}
    public string Name {get;set;}

    public List<DepartmentEmployee> DepartmentEmployees {get;set;}
}

public DepartmentEmployee 
{
    public int DepartmentId {get;set;}
    public Department Department {get;set;}

    public int EmployeeId {get;set;}
    public Employee Employee {get;set;}
}
公共课部
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表部门员工{get;set;}
}
公营雇员
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表部门员工{get;set;}
}
公共部门雇员
{
public int DepartmentId{get;set;}
公共部门部门{get;set;}
public int EmployeeId{get;set;}
公共雇员雇员{get;set;}
}
我们使用PostgreSQL作为数据库,并在appsettings.json文件中设置连接字符串。因此,我们必须向DB上下文提供连接字符串,这是我们在启动时在DI中配置的。但是,因为我们不能为迁移类使用带参数的构造函数,所以我无法将DB上下文注入迁移类


那么,如何从Employee表中读取现有的departmentid和Employee ID,并在反向迁移方法“Up”和“Down”期间将它们添加到新的“DepartmentEmployee”表中呢

您可以尝试使用fluent api,您可以在文章中找到一个现有数据库的示例。@nzrytmn,我使用的是EF Core,给定的链接讨论的是EF6。I我相信您可以通过使用MigrationBuilder.Sql()实现此目标,如@Hitesh所述。您是如何做到的?