創(chuàng)建數(shù)據(jù)庫遷移
返回總目錄《一步一步使用ABP框架搭建正式項(xiàng)目系列教程》
這一節(jié)我們說說數(shù)據(jù)庫遷移(Migration)。
我們之前用的DBFirst創(chuàng)建了實(shí)體類,但當(dāng)初這么做的原因是為了節(jié)省時(shí)間。現(xiàn)在我們通過創(chuàng)建的實(shí)體類和DbContext類利用EF的Code First數(shù)據(jù)庫遷移反過來創(chuàng)建數(shù)據(jù)庫。ABP模板默認(rèn)開啟了遷移,并且添加了一下下面的Configuration類:
namespace Noah.ChargeStation.Migrations { internal sealed class Configuration : DbMigrationsConfiguration<ChargeStation.EntityFramework.ChargeStationDbContext> { public Configuration() { AutomaticMigrationsEnabled = false; ContextKey = "ChargeStation"; } /// <summary> /// 添加種子數(shù)據(jù),比如默認(rèn)管理員等數(shù)據(jù) /// </summary> /// <param name="context">當(dāng)前數(shù)據(jù)庫上下文子類</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(); } } } }
在Seed方法中,添加了租戶,角色和用戶數(shù)據(jù)。現(xiàn)在,我來創(chuàng)建初始化遷移。打開包管理器控制臺,輸入下面的命令:
此處特別注意,紅色方框中一定不要忘了選擇EF項(xiàng)目,否則不會有下面出現(xiàn)的命令“Add-Migration”,”InitialData”是生成文件的后綴名(也是文件中類的名字),也可以取其他名字。
可以看到生成的文件一個(gè)以cs結(jié)尾,這里面的代碼是創(chuàng)建數(shù)據(jù)庫中表的,另一個(gè)以Designer.cs結(jié)尾,記錄的是數(shù)據(jù)庫遷移的版本記錄,最后一個(gè)以.resx文件是資源文件,暫且不需要考慮。
剛才我們只是創(chuàng)建了創(chuàng)建數(shù)據(jù)庫所需要的類,但還沒有創(chuàng)建數(shù)據(jù)庫。為了創(chuàng)建數(shù)據(jù)庫,需要在包管理控制臺執(zhí)行以下命令:
PM> Update-Database
該命令完成了這次數(shù)據(jù)庫的遷移,創(chuàng)建了數(shù)據(jù)庫并填充了種子數(shù)據(jù)。
當(dāng)我們改變實(shí)體類時(shí),可以使用Add-Migration命令創(chuàng)建新的遷移類和Update-Database命令更新數(shù)據(jù)庫。
至此,數(shù)據(jù)庫遷移完成。下一次我們說說《定義倉儲》。
已將所有贊助者統(tǒng)一放到單獨(dú)頁面!簽名處只保留最近10條贊助記錄!查看贊助者列表
| 衷心感謝打賞者的厚愛與支持!也感謝點(diǎn)贊和評論的園友的支持! | |||
|---|---|---|---|
| 打賞者 | 打賞金額 | 打賞日期 | |
| 微信:匿名 | 10.00 | 2017-08-03 | |
| 微信:匿名 | 10.00 | 2017-08-04 | |
| 微信:匿名 | 5.00 | 2017-06-15 | |
| 支付寶:一個(gè)名字499***@qq.com | 5.00 | 2017-06-14 | |
| 微信:匿名 | 16.00 | 2017-04-08 | |
| 支付寶:向京劉 | 10.00 | 2017-04-13 | |
| 微信:匿名 | 10.00 | 2017-003-08 | |
| 微信:匿名 | 5.00 | 2017-03-08 | |
| 支付寶:lll20001155 | 5.00 | 2017-03-03 | |
| 支付寶:她是一個(gè)弱女子 | 5.00 | 2017-03-02 | |





浙公網(wǎng)安備 33010602011771號