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
- EF6 CodeFirst 多个DbContexts
- EF6中CodeFirst使用MySQL
- vs2013+EF6+mysql codefirst 数据迁移
- EF6(CodeFirst)+MySql开发脱坑指南
- CodeFirst
- CodeFirst
- EF6 在原有数据库中使用 CodeFirst 总复习(一、搭建基础环境)
- EF6 在原有数据库中使用 CodeFirst 总复习(三、重建迁移)
- EF6 在原有数据库中使用 CodeFirst 总复习(四、新建实体对象)
- EF6 在原有数据库中使用 CodeFirst 总复习(五、生成发帖页面)
- EF6 在原有数据库中使用 CodeFirst 总复习(一、搭建基础环境)
- EF6
- EF6 在原有数据库中使用 CodeFirst 总复习(二、新的需求,简单修改原有表)
- EF6 在原有数据库中使用 CodeFirst 总复习(二、新的需求,简单修改原有表)
- MVC5与EF6 Code First 第一个入门完整实例教程
- MVC5与EF6 Code First 第一个入门完整实例教程
- C# EF CodeFirst简单工厂模式,接口多继承实例
- MVC codefirst
- CentOS7.3安装单节点Oracle 12c数据库详细配置操作
- 计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-总结及部分代码
- jeesite中的ztree应用
- 2017 icpc 乌鲁木齐赛区 F.Islands(强连通图模板)
- 代码质量的有力保证---SonarLint
- EF6 CodeFirst 多个DbContexts
- 设计模式之-单例设计模式
- linux中vi/vim教程
- 深度学习论文资源(截至2016年)
- Git多人协作开发
- mysql常用法
- 深度学习篇——Ubuntu16.04下安装caffe(GPU加速)
- HTTP简单介绍
- 如何在IAR中通过Watch窗口观察局部变量的值