【问题标题】:Insert data simultaneously in entity having one to many relationship在具有一对多关系的实体中同时插入数据
【发布时间】:2013-12-20 19:15:32
【问题描述】:

我有两个实体:学生和注册具有一对多关系。 实体看起来像

public class Student{
 [Key]
 public int Studentid {get; set;}
 public string fname{get; set;}
 public string lname{get; set;}
 public virtual ICollection<Enrollement> Enrollement_E{ get; set; }
 }

 public class Enrollement{
 [Key]
 public int EnrolId {get; set;}

 [ForeignKey("Studentid ")]
 public int Student{get; set;}
 public string kk{get; set;}
 public virtual Student Student_E { get; set; }
 }

Studentid 是一个自动递增的主键,在登记表中用作外键。我正在尝试将数据插入两个表中。如果任一 1 失败,则两个插入都不应该提交。所以,我有以下代码。

try{
  repository.stu.insert(new student{fname = "fname", lname="lname"});
  repository.enr.insert(new Enrollement{student=???, kk="test"});
}
finally{
  repository.save();
}

当数据尚未保存时,我应该如何将外键传递给 Enrollement 记录。或者有其他方法吗?

【问题讨论】:

    标签: c# asp.net-mvc entity-framework insert one-to-many


    【解决方案1】:

    分配整个对象,而不是分配外键:

    try{
      var student = new student(){fname = "fname", lname="lname"};
      repository.stu.insert(student);
      repository.enr.insert(new Enrollement(){Student_E = student});
    }
    finally{
      repository.save();
    }
    

    这样,当student 获得它的新密钥时,它将自动保存到注册中。

    【讨论】:

    • 谢谢。你的意思是 repository.enr.insert(new Enrollement{studentId=student.Id});?
    • 没有。您必须分配关系对象。您如何从 Enrollment() 访问学生?
    • 你的外国实体是Student_E
    【解决方案2】:

    你不需要单独保存这两个对象,你可以通过只保存调用来实现这一点

    public class Student{
     [Key]
     public int Studentid {get; set;}
     public string fname{get; set;}
     public string lname{get; set;}
     public virtual ICollection<Enrollement> Enrollement_E{ get; set; }
     }
    
     public class Enrollement{
     [Key]
     public int EnrolId {get; set;}
     [ForeignKey("Studentid ")]
     public int Student{get; set;}
     public string kk{get; set;}
     public virtual Student Student_E { get; set; }
     }
    

    在连接的 OnModelCreating 方法中,定义类似的关系,它将确保当你有 Student 对象时,它的相关或子对象,即 Enrollements 也将被保存

    modelBuilder.Entity<Student>()
                    .HasMany(c => c. Enrollement_E)
                    .WithOne(e => e. Student_E);
    modelBuilder.Entity<Enrollement>().Ignore(x => x. Student_E);
    

    现在你的代码应该是这样的

    try{
         student _student = new student{fname = "fname", lname="lname"};
         _student.Enrollement_E = new List< Enrollement>();
         _student.Enrollement_E.add(new Enrollement{student=???, kk="test"});
         repository.stu.insert(_student); 
    }
    finally
    {
         repository.save();
    }
    

    这是使用 Entity Framework Core 完成的 最后一件事,遵循约定,您还需要更改外键字段

    public int Student{get; set;} to  public int StudentId{get; set;}
    

    【讨论】:

      【解决方案3】:

      Ef 轻松处理外键,将数据插入多个相关表。如果您想在两个表中同时插入数据,可以使用以下代码进行插入:

         student st = new student 
          {
            fname = "fname",
            lname= "lname",
      
            Enrollement_E=new List<Enrollement>
            {
              new Enrollement{kk="something"}
            }
          };
      
          repository.student.Add(st);
          repository.SaveChanges();
      

      Ef 本身为第二个表生成外键

      【讨论】:

        猜你喜欢
        • 2011-07-21
        • 1970-01-01
        • 2020-01-26
        • 2013-01-12
        • 1970-01-01
        • 1970-01-01
        • 2020-05-27
        • 2012-09-29
        • 1970-01-01
        相关资源
        最近更新 更多