zoukankan      html  css  js  c++  java
  • 基于EF创建数据库迁移

    通过创建的实体类和DbContext类利用EF的Code First数据库迁移创建数据库。

    下面看代码。

    一、先创建实体类

    我先添加一个BaseEntity,里面就一个属性

    [Key]
            public virtual Guid Id { set; get; }

    然后User表和Role表

    public class Users : BaseEntity
        {
            public string Name { get; set; }
            public Guid RoleId { get; set; }
            public virtual Roles Role { get; set; }
        }
    public class Roles:BaseEntity
        {
            public string Name { get; set; }
        }

    二、创建DatabaseContext

    public class DatabaseContext : DbContext
        {
            public DatabaseContext() : base("name=conn")
            {
    
            }
    
            public virtual DbSet<Users> Users { get; set; }
            public virtual DbSet<Roles> Roles { get; set; }
        }

    这里DbSet添加我要操作的实体。

    三、创建Configuration

    public class Configuration : DbMigrationsConfiguration<DatabaseContext>
        {
            public Configuration()
            {
                AutomaticMigrationsEnabled = false;
                ContextKey = "AbpDemo";
            }
    
            protected override void Seed(DatabaseContext context)
            {
                //base.Seed(context);
                new DefaultRoleAndUserBuilder(context).Build();//后面添加默认数据用的
            }
        }

    添加默认数据用的。比如添加管理员

    四、添加默认数据生成类

    public class DefaultRoleAndUserBuilder
        {
            private readonly DatabaseContext _context;
            public DefaultRoleAndUserBuilder(DatabaseContext context)
            {
                _context = context;
            }
    
            public void Build()
            {
                CreateUserAndRoles();
            }
    
            private void CreateUserAndRoles()
            {
                var adminRole = _context.Roles.FirstOrDefault(x => x.Name == "admin");
                if (adminRole == null)
                {
                    adminRole = _context.Roles.Add(new Models.Roles { Id = Guid.NewGuid(), Name = "admin" });
                    _context.SaveChanges();
                }
    
                var adminUser = _context.Users.FirstOrDefault(x => x.Name == "admin");
                if (adminUser == null)
                {
                    adminUser = _context.Users.Add(new Models.Users { Id = Guid.NewGuid(), Name = "admin", RoleId = adminRole.Id });
    
                    _context.SaveChanges();
                }
            }
        }

    五、生成数据库

    打开程序包管理器控制台,执行命令

    add-migration "InitialData"

    这是生成的cs文件的后缀名,执行完后会发现Migrations文件夹多了一个文件,但是数据库没有生成,看下图。

    继续执行命令

    update-database

    这样数据库也有了。

  • 相关阅读:
    深入解析委托和事件
    一个小型工程报价系统(三层架构)
    DONET三层架构开发初步
    VS项目重命名工具
    Visual Studio 2012 应用软件开发新方式
    Consumer is not subscribed to any topics or assigned any partitions
    kafka的一些常用命令
    横向遍历二叉树
    Flume的断点续传解决
    实际生产用法CMS和G1
  • 原文地址:https://www.cnblogs.com/xiaoquangege/p/6117346.html
Copyright © 2011-2022 走看看