AbpVnext系列<三> 添加种子项目

一、src下面增加DbMigrations类库,注意是要.Net Framework 类型的类库,不能是.Net Standard 的。

 二、添加类库后为项目添加如上三个项目

appsetting.json    配置信息

{
  "ConnectionStrings": {
    "AidenAdmin": "Server=127.0.0.1;port=3306;Database=AidenAdmin;User=root; Password=root"
  }

}
View Code

DbM_AidenAdminDbContext    上下文

namespace AidenAdmin.DbMigrations
{
    [ConnectionStringName(AidenAdminDbProperties.ConnectionStringName)]
    public class DbM_AidenAdminDbContext : AidenAdminDbContext
    {
        public DbM_AidenAdminDbContext(DbContextOptions<AidenAdminDbContext> options)
            : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

        }
    }
}
View Code

AidenAdminDbMigrationsContextFactory   上下文初始化文件

 public class AidenAdminDbMigrationsContextFactory : IDesignTimeDbContextFactory<DbM_AidenAdminDbContext>
    {
        public DbM_AidenAdminDbContext CreateDbContext(string[] args)
        {
            var configuration = BuildConfiguration();

            var builder = new DbContextOptionsBuilder<AidenAdminDbContext>()
                .UseMySql(ServerVersion.AutoDetect("AidenAdmin"));

            return new DbM_AidenAdminDbContext(builder.Options);
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false);

            return builder.Build();
        }
    }
View Code

 

posted @ 2024-06-22 08:30  许轩霖  阅读(21)  评论(0编辑  收藏  举报