EF6 CodeFirst 多个DbContexts

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
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值