在一个项目中,使用了多个 DbContext
且使用同一个数据库的状况数据库
单击“肯定”c#
将此文件的内容替换为如下代码:cookie
using System.Collections.Generic; using Microsoft.EntityFrameworkCore; namespace WebApplication.Models { public class FirstDbContext : DbContext { public FirstDbContext(DbContextOptions<FirstDbContext> options) : base(options) { } public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public ICollection<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } } }
生产应用一般会将每一个类放在单独的文件中。 为简单起见,本教程将这些类放在一个文件中。函数
将此文件的内容替换为如下代码:工具
using Microsoft.EntityFrameworkCore; namespace WebApplication.Models { public class SecondDbContext : DbContext { public SecondDbContext(DbContextOptions<SecondDbContext> options) : base(options) { } public DbSet<Student> Students { get; set; } } public class Student { public int Id { get; set; } public string Name { get; set; } } }
生产应用一般会将每一个类放在单独的文件中。 为简单起见,本教程将这些类放在一个文件中。spa
至此,项目的目录结构以下:code
若要使 FirstDbContext
和 SecondDbContext
可用于 MVC 控制器,请在 Startup.cs
中将其注册为服务。blog
在应用程序启动过程当中,经过依赖关系注入 注册服务(如 FirstDbContext),以便可以经过构造函数的参数和属性向使用服务的组件(如 MVC 控制器)自动提供该服务。教程
在 Startup.cs 中,添加如下 using 语句:
using WebApplication.Models; using Microsoft.EntityFrameworkCore;
将如下 手动高亮
的代码添加到 ConfigureServices
方法:
public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); var connection = @"Server=你的数据库地址;Database=MultipleDbContext;User Id=你的数据库帐号;Password=你的数据库密码;"; // 手动高亮 services.AddDbContext<FirstDbContext> // 手动高亮 (options => options.UseSqlServer(connection, x => x.MigrationsHistoryTable("__FirstDbMigrationsHistory"))); // 手动高亮 services.AddDbContext<SecondDbContext> // 手动高亮 (options => options.UseSqlServer(connection, x => x.MigrationsHistoryTable("__SecondDbMigrationsHistory"))); // 手动高亮 }
生产应用一般会将链接字符串放在配置文件或环境变量中。 为简单起见,本教程在代码中定义它。
如下步骤使用迁移建立数据库。
运行如下命令建立 FirstDbContext
的迁移:
Add-Migration InitialCreate -Context FirstDbContext -OutputDir Migrations\FirstDbContextMigrations Update-Database -Context FirstDbContext
-Context
DbContext
类,请参阅
这里了解详细信息。
运行如下命令建立 SecondDbContext
的迁移:
Add-Migration InitialCreate -Context SecondDbContext -OutputDir Migrations\SecondDbContextMigrations Update-Database -Context SecondDbContext
至此,项目的目录结构以下:
数据库以下:
请避免两个 DBContext
内的实体有互相主外键链接的状况
示例
// FirstDbContext public class FirstDbContext : DbContext { public FirstDbContext(DbContextOptions<FirstDbContext> options) : base(options) { } public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public ICollection<Post> Posts { get; set; } public int StudentId { get; set; } public Student Student { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } } // SecondDbContext public class SecondDbContext : DbContext { public SecondDbContext(DbContextOptions<SecondDbContext> options) : base(options) { } public DbSet<Student> Students { get; set; } } public class Student { public int Id { get; set; } public string Name { get; set; } public ICollection<Blog> Blogs { get; set; } }