将两个记录同时插入到两个表中,其中一个表使用另一个表作为外键?

时间:2021-07-09 14:16:34

I have two tables:

我有两个表:

  • Employee: EmpId (PK), EmpName .
  • 员工:EmpId (PK), EmpName。
  • EmployeeDetails : Id (auto increment), EmpId (FK to Employee table), Address, ZipCode .
  • 雇员详细信息:Id(自动增加),EmpId (FK到员工表),地址,邮政编码。

I'm using Entity Framework 6.

我使用的是实体框架6。

When I want to insert a new Employee, I must do two transactions, meaning use SaveChanges() twice, in order to insert a new employee and only then use its Id as a foreign key in EmployeeDetails .

当我想插入一个新员工时,我必须执行两个事务,即使用SaveChanges()两次,以便插入一个新员工,然后在EmployeeDetails中使用其Id作为外键。

Is is possible to do that in one transaction ?

是否可以在一个事务中做到这一点?

Thanks

谢谢

2 个解决方案

#1


3  

You can easily do this in a single step:

你可以很容易地做到这一步:

using (YourDbContext ctx = new YourDbContext())
{
    Employee emp = new Employee();
    // set the values for "emp"

    emp.EmployeeDetail = new EmployeeDetails();
    // set the employee details

    ctx.SaveChanges();
}

If you create the EmployeeDetails as part of the Employee, you can just save the Employee alone - and EF will store both entities (as an "entity graph") and set up the FK constraints between them properly - all in a single step

如果您将EmployeeDetails创建为雇员的一部分,您可以单独保存该雇员——EF将存储两个实体(作为“实体图”),并在它们之间正确地设置FK约束——所有这些都是在单个步骤中完成的

#2


1  

You should modify your model a bit. Details is "part" of Employee. If you create the entities in this way, when you insert an employee, entity framework will take care of the rest for you:

您应该稍微修改一下您的模型。细节是员工的“一部分”。如果您以这种方式创建实体,当您插入员工时,实体框架将为您处理其余部分:

Employee

员工

public class Employee
{
    public int EmpId { get; set; }

    public string EmpName { get; set; }

    public EmployeeDetails  EmployeeDetails { get; set; }
}

Details

细节

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

Than you can do this:

而不是你能做到的:

using (dbContext context = new dbContext())
{
    Employee emp = new Employee();

    emp.EmpName = "John";
    emp.EmployeeDetails = new EmployeeDetails 
    {
        //details fields
    };

    context.SaveChanges();
}

#1


3  

You can easily do this in a single step:

你可以很容易地做到这一步:

using (YourDbContext ctx = new YourDbContext())
{
    Employee emp = new Employee();
    // set the values for "emp"

    emp.EmployeeDetail = new EmployeeDetails();
    // set the employee details

    ctx.SaveChanges();
}

If you create the EmployeeDetails as part of the Employee, you can just save the Employee alone - and EF will store both entities (as an "entity graph") and set up the FK constraints between them properly - all in a single step

如果您将EmployeeDetails创建为雇员的一部分,您可以单独保存该雇员——EF将存储两个实体(作为“实体图”),并在它们之间正确地设置FK约束——所有这些都是在单个步骤中完成的

#2


1  

You should modify your model a bit. Details is "part" of Employee. If you create the entities in this way, when you insert an employee, entity framework will take care of the rest for you:

您应该稍微修改一下您的模型。细节是员工的“一部分”。如果您以这种方式创建实体,当您插入员工时,实体框架将为您处理其余部分:

Employee

员工

public class Employee
{
    public int EmpId { get; set; }

    public string EmpName { get; set; }

    public EmployeeDetails  EmployeeDetails { get; set; }
}

Details

细节

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

Than you can do this:

而不是你能做到的:

using (dbContext context = new dbContext())
{
    Employee emp = new Employee();

    emp.EmpName = "John";
    emp.EmployeeDetails = new EmployeeDetails 
    {
        //details fields
    };

    context.SaveChanges();
}