当前位置: 移动技术网 > IT编程>开发语言>.net > EF Core 2.2 对多个 DbContext 单个数据库的情况进行迁移的示例

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

2019年04月11日  | 移动技术网IT编程  | 我要评论

猎杀禁地,建行贷款利率,厦门舞蹈学校

目录

场景

在一个项目中,使用了多个 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 应用程序(模型视图控制器)”项目模板
  • 确保将“身份验证”设置为“无身份验证”
  • 单击“确定”

创建第一个模型

  • 右键单击“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; }
        }
    }

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

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

使用依赖注入注册上下文

若要使 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=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"))); // 手动高亮
        }

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

创建数据库

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

  • “工具”>“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
  • 至此,项目的目录结构如下:

  • 数据库如下:

需要注意的情况

请避免两个 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; }
    }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网