Database Initialization Strategies in Code-First & Seed Data(sample role)
2015-08-12 15:38
507 查看
You already created a database after running your Code-First application the first time, but what about the second time onwards?? Will it create a new database every time you run the application? What about the production environment? How do you alter the databasewhen you change your domain model? To handle these scenarios, you have to use one of the database initialization strategies.There are four different database initialization strategies:CreateDatabaseIfNotExists: This is default initializer. As the name suggests, it will create the database if none exists as per the configuration. However, if you change the model class and thenrun the application with this initializer, then it will throw an exception.DropCreateDatabaseIfModelChanges: This initializer drops an existing database and creates a new database, if your model classes (entity classes) have been changed. So you don't have to worry about maintainingyour database schema, when your model classes change.DropCreateDatabaseAlways: As the name suggests, this initializer drops an existing database every time you run the application, irrespective of whether your model classes have changed or not. This will be useful,when you want fresh database, every time you run the application, like while you are developing the application.Custom DB Initializer: You can also create your own custom initializer, if any of the above doesn't satisfy your requirements or you want to do some other process that initializes the database using the aboveinitializer.To use one of the above DB initialization strategies, you have to set the DB Initializer using Database class in Context class, as shown below:
public class SchoolDBContext: DbContext { public SchoolDBContext(): base("SchoolDBConnectionString") { Database.SetInitializer<SchoolDBContext>(new CreateDatabaseIfNotExists<SchoolDBContext>()); //Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseIfModelChanges<SchoolDBContext>()); //Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseAlways<SchoolDBContext>()); //Database.SetInitializer<SchoolDBContext>(new SchoolDBInitializer()); } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } }You can also create your custom DB initializer, by inheriting one of the initializers, as shown below:
public class SchoolDBInitializer : CreateDatabaseIfNotExists<SchoolDBContext> { protected override void Seed(SchoolDBContext context) { base.Seed(context); } }As you can see in the above code, we have created a new class SchoolDBInitializer, which is derived from DropCreateDatabaseAlways initializer.
Set db initializer in the configuration file:
You can also set db initializer in the configuration file. For example, to set default initializer in app.config:<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="DatabaseInitializerForType SchoolDataLayer.SchoolDBContext, SchoolDataLayer" value="System.Data.Entity.DropCreateDatabaseAlways`1[[SchoolDataLayer.SchoolDBContext, SchoolDataLayer]], EntityFramework" /> </appSettings> </configuration>You can set custom DB initializer, as follows:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="DatabaseInitializerForType SchoolDataLayer.SchoolDBContext, SchoolDataLayer" value="SchoolDataLayer.SchoolDBInitializer, SchoolDataLayer" /> </appSettings> </configuration>
string roleName = applicationRole.ToString();IdentityResult roleResult;// Check to see if Role Exists, if not create itif (!RoleManager.RoleExists(roleName)){roleResult = RoleManager.Create(new IdentityRole(roleName));}This way, you can use DB initialization strategy for your application.
相关文章推荐
- 在.gitignore中过滤不必要的文件
- 如何查看mysql运行、访问记录等日志
- IOS 的material design汇总
- IOS远程推送通知
- 多校第七场 1010 hdu 5378 Leader in Tree Land(概率dp)
- bzoj-1001 狼抓兔子
- 好用的eclipse properties插件
- [hdu4629 Burning]三角形面积并,扫描线
- socket学习笔记——IO口的基本操作(读、写)
- 【JDK配置原创】JDK(JRE)环境变量配置原理 --费元星
- Memcache 问题集锦
- iOS项目开发实战——通过Http Get方式与服务器通信
- 8583报文MAC验证实现过程
- 用Git 进行分布式代码管理
- 如何在Windows7上使用Hyper-V
- JSP EL表达式详细介绍
- Yii2.0中文开发向导——查询条件Where全解析
- 简单的MySQL备份与还原方法分享
- 组合数学的一些常见公式
- 华为OJ(挑7)