欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

EF Core 2.2 对多个 DbContext 多个数据库的情况进行迁移的示例

程序员文章站 2022-07-11 08:40:37
[TOC] 场景 在一个项目中,使用了多个 且每个 对应一个数据库的情况 创建新项目 打开 Visual Studio 2017 “文件” “新建” “项目” 从左菜单中选择“已安装” “Visual C ” “.NET Core”。 选择“ASP.NET Core Web 应用程序”。 输入“We ......

目录

场景

在一个项目中,使用了多个 dbcontext 且每个 dbcontext 对应一个数据库的情况

创建新项目

  • 打开 visual studio 2017
  • “文件”>“新建”>“项目”
  • 从左菜单中选择“已安装”>“visual c#”>“.net core”。
  • 选择“asp.net core web 应用程序”。
  • 输入“webapplication”作为名称,然后单击“确定”。
  • 在“新建 asp.net core web 应用程序”对话框中:
  • 确保在下拉列表中选择“.net core”和“asp.net core 2.1”
  • 选择“web 应用程序(模型视图控制器)”项目模板
  • 确保将“身份验证”设置为“无身份验证”
  • 单击“确定”

    EF Core 2.2 对多个 DbContext 多个数据库的情况进行迁移的示例

    EF Core 2.2 对多个 DbContext 多个数据库的情况进行迁移的示例

创建第一个模型

  • 右键单击“models”文件夹,然后选择“添加”>“类”。
  • 输入“firstmodel.cs”作为名称,然后单击“确定”。
  • 将此文件的内容替换为以下代码:

    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; }
        }
    }

    生产应用通常会将每个类放在单独的文件中。 为简单起见,本教程将这些类放在一个文件中。

创建第二个模型

  • 右键单击“models”文件夹,然后选择“添加”>“类”。
  • 输入“secondmodel.cs”作为名称,然后单击“确定”。
  • 将此文件的内容替换为以下代码:

    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; }
        }
    }

    生产应用通常会将每个类放在单独的文件中。 为简单起见,本教程将这些类放在一个文件中。

  • 至此,项目的目录结构如下:

    EF Core 2.2 对多个 DbContext 多个数据库的情况进行迁移的示例

使用依赖注入注册上下文

若要使 firstdbcontextseconddbcontext 可用于 mvc 控制器,请在 startup.cs 中将其注册为服务。

在应用程序启动过程中,通过依赖关系注入 注册服务(如 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=firstdbcontext;user id=你的数据库账号;password=你的数据库密码;"; // 手动高亮
            services.adddbcontext<firstdbcontext> // 手动高亮
                (options => options.usesqlserver(connection)); // 手动高亮
    
            var seconddbconnection = @"server=你的数据库地址;database=seconddbcontext;user id=你的数据库账号;password=你的数据库密码;"; // 手动高亮
            services.adddbcontext<seconddbcontext> // 手动高亮
                (options => options.usesqlserver(seconddbconnection)); // 手动高亮
        }

    生产应用通常会将连接字符串放在配置文件或环境变量中。 为简单起见,本教程在代码中定义它。

创建数据库

以下步骤使用迁移创建数据库。

  • “工具”>“nuget 包管理器”>“包管理器控制台”
  • 运行以下命令创建 firstdbcontext 的迁移:

    add-migration initialcreate -context firstdbcontext -outputdir migrations\firstdbcontextmigrations
    update-database -context firstdbcontext

    -context 参数表示要使用的 dbcontext 类,请参阅了解详细信息。

  • “工具”>“nuget 包管理器”>“包管理器控制台”
  • 运行以下命令创建 seconddbcontext 的迁移:

    add-migration initialcreate -context seconddbcontext -outputdir migrations\seconddbcontextmigrations
    update-database -context seconddbcontext
  • 至此,项目的目录结构如下:

    EF Core 2.2 对多个 DbContext 多个数据库的情况进行迁移的示例

  • 数据库如下:

    EF Core 2.2 对多个 DbContext 多个数据库的情况进行迁移的示例