EF6 CodeFirst 多个DbContexts

来源:互联网 发布:网络零售业现状 编辑:程序博客网 时间:2024/06/15 04:52

Models文件夹

Student.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EFCodeFirstSample.Models{    public class Student    {        public int StudentId { get; set; }        public string StudentName { get; set; }    }}

Teacher.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EFCodeFirstSample.Models{    public class Teacher    {        public int TeacherId { get; set; }        public string TeacherName { get; set; }    }}

DBContexts文件夹

StudentDbContext.cs

using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;using EFCodeFirstSample.Models;namespace EFCodeFirstSample.DBContexts{    public class StudentDbContext: DbContext    {        public StudentDbContext()             : base("EFCodeFirstConnString")         {        }        public DbSet<Student> Students { get; set; }    }}

TeacherDbContext.cs

using System.Data.Entity;using EFCodeFirstSample.Models;namespace EFCodeFirstSample.DBContexts{    public class TeacherDbContext : DbContext    {        public TeacherDbContext()            : base("EFCodeFirstConnString")        {         }        public DbSet<Teacher> Teachers { get; set; }    }}

Migrations文件夹

Configuration.cs

namespace EFCodeFirstSample.Migrations{    using System;    using System.Data.Entity;    using System.Data.Entity.Migrations;    using System.Linq;    using EFCodeFirstSample.Models;    internal sealed class Configuration : DbMigrationsConfiguration<EFCodeFirstSample.DBContexts.StudentDbContext>    {        public Configuration()        {            AutomaticMigrationsEnabled = false;            ContextKey = "EFCodeFirstSample.DBContexts.StudentDbContext";        }        protected override void Seed(EFCodeFirstSample.DBContexts.StudentDbContext context)        {            //迁移到最新版本后将调用此方法。            //您可以使用 DbSet<T>.AddOrUpdate() 辅助扩展方法来避免创建重复的种子数据。 例如。            //    context.People.AddOrUpdate(            //      p => p.FullName,            //      new Person { FullName = "Andrew Peters" },            //      new Person { FullName = "Brice Lambson" },            //      new Person { FullName = "Rowan Miller" }            //    );            //            context.Students.AddOrUpdate(              p => p.StudentName,              new Student { StudentName = "Seed Student Name" }            );        }    }}

TeacherMigrations文件夹

Configuration.cs

namespace EFCodeFirstSample.TeacherMigrations{    using System;    using System.Data.Entity;    using System.Data.Entity.Migrations;    using System.Linq;    using EFCodeFirstSample.Models;    internal sealed class Configuration : DbMigrationsConfiguration<EFCodeFirstSample.DBContexts.TeacherDbContext>    {        public Configuration()        {            AutomaticMigrationsEnabled = false;            MigrationsDirectory = @"TeacherMigrations";            ContextKey = "EFCodeFirstSample.DBContexts.TeacherDbContext";        }        protected override void Seed(EFCodeFirstSample.DBContexts.TeacherDbContext context)        {            //迁移到最新版本后将调用此方法。            //您可以使用 DbSet<T>.AddOrUpdate() 辅助扩展方法来避免创建重复的种子数据。例如。            //    context.People.AddOrUpdate(            //      p => p.FullName,            //      new Person { FullName = "Andrew Peters" },            //      new Person { FullName = "Brice Lambson" },            //      new Person { FullName = "Rowan Miller" }            //    );            //            context.Teachers.AddOrUpdate(              p => p.TeacherName,              new Teacher { TeacherName = "Seed Teacher Name" }            );        }    }}

Program.cs

using System;using System.Data.Entity;using EFCodeFirstSample.DBContexts;using EFCodeFirstSample.Migrations;using EFCodeFirstSample.Models;namespace EFCodeFirstSample{    class Program    {        static void Main(string[] args)        {            Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentDbContext, Configuration>());            Database.SetInitializer(new MigrateDatabaseToLatestVersion<TeacherDbContext, TeacherMigrations.Configuration>());            using (var studentContext = new StudentDbContext())            {                Console.Write("请输入学生的名字: ");                var name = Console.ReadLine();                var student = new Student()                {                    StudentName = name                };                studentContext.Students.Add(student);                studentContext.SaveChanges();            }            using (var teacherContext = new TeacherDbContext())            {                Console.Write("请输入老师的名字: ");                var name = Console.ReadLine();                var teacher = new Teacher()                {                    TeacherName = name                };                teacherContext.Teachers.Add(teacher);                teacherContext.SaveChanges();            }            Console.ReadLine();        }    }}

提示:

对于单个DbContext迁移步骤的命令:
1. Enable-Migrations.它将使用文件Configuraton.cs创建一个名为Migration的文件夹.
2. .如果任何上下文实体更改,请执行Add-Migration <您的迁移名称>;
3. Update-Database -Verbose

对于多个DbContexts迁移步骤的命令:
1.enable-migrations -ContextTypeName <DbContext-Name-with-Namespaces> MigrationsDirectory:<Migrations-Directory-Name>. 这里我们需要定义不同的文件夹,因为我们需要多个配置。

2.Add-Migration -configuration <DbContext-Migrations-Configuration-Class-withNamespaces> <Migrations-Name>

3.Update-Database -configuration <DbContext-Migrations-Configuration-Class-withNamespaces> -Verbose

运行结果如图:

这里写图片描述


这里写图片描述

阅读全文
0 0
原创粉丝点击