Create Database migration and database migration

Source: Internet
Author: User

Create Database migration and database migration
Go back to the General Directory, "Step by Step Using the ABP framework to build a formal project series tutorial"

In this section, we will talk about database Migration (Migration ).

We used DBFirst to create entity classes, but the reason for doing so was to save time. Now we use the created entity class and DbContext class to migrate the Code First database of EF to create a database in turn. By default, the TTL template enables migration and adds the Configuration class under the environment:

Namespace Noah. chargeStation. migrations {internal sealed class Configuration: DbMigrationsConfiguration <ChargeStation. entityFramework. chargeStationDbContext> {public Configuration () {AutomaticMigrationsEnabled = false; ContextKey = "ChargeStation" ;}/// <summary> // Add seed data, for example, the default Administrator and other data /// </summary> /// <param name = "context"> context subclass of the current database </param> protected override void Seed (ChargeStation. entityFramework. chargeStationDbContext context) {context. disableAllFilters (); new InitialDataBuilder (context ). build ();}}}
namespace Noah.ChargeStation.Migrations.SeedData{    public class DefaultTenantRoleAndUserBuilder    {        private readonly ChargeStationDbContext _context;        public DefaultTenantRoleAndUserBuilder(ChargeStationDbContext context)        {            _context = context;        }        public void Build()        {            CreateUserAndRoles();        }        private void CreateUserAndRoles()        {            //Admin role for tenancy owner            var adminRoleForTenancyOwner = _context.Roles.FirstOrDefault(r => r.TenantId == null && r.Name == "Admin");            if (adminRoleForTenancyOwner == null)            {                adminRoleForTenancyOwner = _context.Roles.Add(new Role { Name = "Admin", DisplayName = "Admin", IsStatic = true });                _context.SaveChanges();            }            //Admin user for tenancy owner            var adminUserForTenancyOwner = _context.Users.FirstOrDefault(u => u.TenantId == null && u.UserName == "admin");            if (adminUserForTenancyOwner == null)            {                adminUserForTenancyOwner = _context.Users.Add(                    new User                    {                        TenantId = null,                        UserName = "admin",                        Name = "System",                        Surname = "Administrator",                        EmailAddress = "admin@aspnetboilerplate.com",                        IsEmailConfirmed = true,                        Password = "AM4OLBpptxBYmM79lGOX9egzZk3vIQU3d/gFCJzaBjAPXzYIK3tQ2N7X4fcrHtElTw==" //123qwe                    });                _context.SaveChanges();                _context.UserRoles.Add(new UserRole(adminUserForTenancyOwner.Id, adminRoleForTenancyOwner.Id));                _context.SaveChanges();            }            //Default tenant            var defaultTenant = _context.Tenants.FirstOrDefault(t => t.TenancyName == "Default");            if (defaultTenant == null)            {                defaultTenant = _context.Tenants.Add(new Tenant { TenancyName = "Default", Name = "Default" });                _context.SaveChanges();            }            //Admin role for 'Default' tenant            var adminRoleForDefaultTenant = _context.Roles.FirstOrDefault(r => r.TenantId == defaultTenant.Id && r.Name == "Admin");            if (adminRoleForDefaultTenant == null)            {                adminRoleForDefaultTenant = _context.Roles.Add(new Role { TenantId = defaultTenant.Id, Name = "Admin", DisplayName = "Admin", IsStatic = true });                _context.SaveChanges();            }            //Admin for 'Default' tenant            var adminUserForDefaultTenant = _context.Users.FirstOrDefault(u => u.TenantId == defaultTenant.Id && u.UserName == "admin");            if (adminUserForDefaultTenant == null)            {                adminUserForDefaultTenant = _context.Users.Add(                    new User                    {                        TenantId = defaultTenant.Id,                        UserName = "admin",                        Name = "System",                        Surname = "Administrator",                        EmailAddress = "admin@aspnetboilerplate.com",                        IsEmailConfirmed = true,                        Password = "AM4OLBpptxBYmM79lGOX9egzZk3vIQU3d/gFCJzaBjAPXzYIK3tQ2N7X4fcrHtElTw==" //123qwe                    });                _context.SaveChanges();                _context.UserRoles.Add(new UserRole(adminUserForDefaultTenant.Id, adminRoleForDefaultTenant.Id));                _context.SaveChanges();            }        }    }}

Added tenant, role, and user data in the Seed method. Now, let's create the initialization migration. OpenPackage Manager Console, Enter the following command:

PM> Update-Database

 

This command migrates the database, creates the database, and fills in the seed data.

When we change the object class, we can use the Add-Migration command to create a new Migration class and the Update-Database command to Update the Database.

So far, the database migration is complete. Next time, let's talk about definition warehousing.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.