📜  Entity Framework-多个DbContext

📅  最后修改于: 2020-11-21 07:37:14             🧑  作者: Mango


在本章中,我们将学习在应用程序中有多个DbContext类时如何将更改迁移到数据库中。

  • 实体框架6.0中首次引入了多个DbContext。
  • 多个上下文类可以属于一个数据库或两个不同的数据库。

在我们的示例中,我们将为同一数据库定义两个Context类。在下面的代码中,有两个适用于Student和Teacher的DbContext类。

public class Student {
   public int ID { get; set; }
   public string LastName { get; set; }
   public string FirstMidName { get; set; }
   public DateTime EnrollmentDate { get; set; }
}

public class MyStudentContext : DbContext {
   public MyStudentContext() : base("UniContextDB") {}
   public virtual DbSet Students { get; set; }
}

public class Teacher {
   public int ID { get; set; }
   public string LastName { get; set; }
   public string FirstMidName { get; set; }
   public DateTime HireDate { get; set; }
}

public class MyTeacherContext : DbContext {
   public MyTeacherContext() : base("UniContextDB") {}
   public virtual DbSet Teachers { get; set; }
}

如您在上面的代码中看到的,有两个模型称为“学生”和“老师”。每个都与一个特定的对应上下文类相关联,即,Student与MyStudentContext相关联,而Teacher与MyTeacherContext相关联。

当同一项目中有多个Context类时,这是迁移数据库中的更改的基本规则。

  • enable-migrations -ContextTypeName MigrationsDirectory:<迁移目录名称>

  • Add-Migration-配置

  • 更新数据库配置-详细

让我们通过在Package Manager Console中执行以下命令来启用MyStudentContext的迁移。

PM→ enable-migrations -ContextTypeName:EFCodeFirstDemo.MyStudentContext

程序包管理器控制台

一旦执行,我们将模型添加到迁移历史中,为此,我们必须在同一控制台中触发add-migration命令。

PM→ add-migration -configuration EFCodeFirstDemo.Migrations.Configuration Initial

现在让我们将一些数据添加到数据库的“学生和教师”表中。

static void Main(string[] args) {

   using (var context = new MyStudentContext()) {
    
      //// Create and save a new Students
      Console.WriteLine("Adding new students");

      var student = new Student {
         FirstMidName = "Alain", 
         LastName = "Bomer", 
         EnrollmentDate = DateTime.Parse(DateTime.Today.ToString())
         //Age = 24
      };

      context.Students.Add(student);

      var student1 = new Student {
         FirstMidName = "Mark",
         LastName = "Upston", 
         EnrollmentDate = DateTime.Parse(DateTime.Today.ToString())
         //Age = 30
      };

      context.Students.Add(student1);
      context.SaveChanges();
        
      // Display all Students from the database
      var students = (from s in context.Students orderby s.FirstMidName
         select s).ToList();
        
      Console.WriteLine("Retrieve all Students from the database:");

      foreach (var stdnt in students) {
         string name = stdnt.FirstMidName + " " + stdnt.LastName;
         Console.WriteLine("ID: {0}, Name: {1}", stdnt.ID, name);
      }

      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
   }

   using (var context = new MyTeacherContext()) {

      //// Create and save a new Teachers
      Console.WriteLine("Adding new teachers");

      var student = new Teacher {
         FirstMidName = "Alain", 
         LastName = "Bomer", 
         HireDate = DateTime.Parse(DateTime.Today.ToString())
         //Age = 24
      };

      context.Teachers.Add(student);

      var student1 = new Teacher {
         FirstMidName = "Mark", 
         LastName = "Upston", 
         HireDate = DateTime.Parse(DateTime.Today.ToString())
         //Age = 30
      };

      context.Teachers.Add(student1);
      context.SaveChanges();
  
      // Display all Teachers from the database
      var teachers = (from t in context.Teachers orderby t.FirstMidName
         select t).ToList();
        
      Console.WriteLine("Retrieve all teachers from the database:");

      foreach (var teacher in teachers) {
         string name = teacher.FirstMidName + " " + teacher.LastName;
         Console.WriteLine("ID: {0}, Name: {1}", teacher.ID, name);
      }

      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
   }
}

执行上述代码后,您将看到为两个不同的模型创建了两个不同的表,如下图所示。

执行代码

我们建议您逐步执行上述示例,以更好地理解。