我们希望当程序运行时自动完成数据库的创建并预置初始值。自己的Context名为 DataBaseContet
有两种方法:
1、在Global.asax中添加 Database.SetInitializer<DataBaseContext>(new DatabaseInitializer());
2、配置文件中添加 contexts 节点, 节点中指定类名与命名空间
<entityFramework> <contexts> <context type="DataBase.DataBaseContext, DataBase" disableDatabaseInitialization="false"> <databaseInitializer type="DataBase.DatabaseInitializer, DataBase" /> </context> </contexts> <defaultConnectionFactory type="DataBase.DataBaseContext, DataBase"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework>
其中 DataBaseContext为:
namespace DataBase { public class DataBaseContext : DbContext { public DataBaseContext() : base("default") { //是否启用延迟加载: // true: 延迟加载(Lazy Loading):获取实体时不会加载其导航属性,一旦用到导航属性就会自动加载 // false: 直接加载(Eager loading):通过 Include 之类的方法显示加载导航属性,获取实体时会即时加载通过 Include 指定的导航属性 this.Configuration.LazyLoadingEnabled = true; this.Configuration.AutoDetectChangesEnabled = true; //自动监测变化,默认值为 true } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } public DbSet<UserModels> UserContext { get; set; } public DbSet<PigModels> PigContext { get; set; } } }
自定义的数据初始化方法:DatabaseInitializer,其中继承的是DropCreateDatabaseAlways 只为演示用, 实际项目中根据需要选择。
namespace DataBase { public class DatabaseInitializer : DropCreateDatabaseAlways<DataBaseContext> { protected override void Seed(DataBaseContext context) { UserModels m1 = new UserModels() { UserGUID = Guid.NewGuid().ToString(), UserName = "张一", UserNumber = "0001", UserBirthDay = "1990.1.1", UserMail = "[email protected]", UserPhone = "13100001111", Password="aaaaaa" }; UserModels m2 = new UserModels() { UserGUID = Guid.NewGuid().ToString(), UserName = "张二", UserNumber = "0002", UserBirthDay = "1989.12.12", UserMail = "[email protected]", UserPhone = "13100002222", Password = "aaaaaa" }; UserModels m3 = new UserModels() { UserGUID = Guid.NewGuid().ToString(), UserName = "张三", UserNumber = "0003", UserBirthDay = "1989.12.12", UserMail = "[email protected]", UserPhone = "13100003333", Password = "aaaaaa" }; try { // 写数据库 context.UserContext.Add(m1); context.UserContext.Add(m2); context.UserContext.Add(m3); context.SaveChanges(); } catch (DbEntityValidationException dbEx) { } base.Seed(context); } } }
具体步骤:
首先通过NuGet工具安装EntityFramework,本人用的为6.0版本。
1、建立实体类:
namespace DatabaseModels { public class UserModels { [Required] [Key] public string UserGUID { get; set; } [Required] [Display(Name = "用户名")] public string UserName { get; set; } [Display(Name = "用户工号")] public string UserNumber { get; set; } [Required] [DataType(DataType.Password)] [Display(Name = "密码")] public string Password { get; set; } [Display(Name = "出生日期")] public string UserBirthDay { get; set; } [Display(Name = "电话号")] public string UserPhone { get; set; } [Display(Name = "邮箱")] public string UserMail { get; set; } } }
2、建立Context 继承 DbContext
namespace DataBase { public class DataBaseContext : DbContext { public DataBaseContext() : base("default") { //是否启用延迟加载: // true: 延迟加载(Lazy Loading):获取实体时不会加载其导航属性,一旦用到导航属性就会自动加载 // false: 直接加载(Eager loading):通过 Include 之类的方法显示加载导航属性,获取实体时会即时加载通过 Include 指定的导航属性 this.Configuration.LazyLoadingEnabled = true; this.Configuration.AutoDetectChangesEnabled = true; //自动监测变化,默认值为 true } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } public DbSet<UserModels> UserContext { get; set; } } }
3、创建数据初始方法
namespace DataBase { public class DatabaseInitializer : DropCreateDatabaseAlways<DataBaseContext> { protected override void Seed(DataBaseContext context) { UserModels m1 = new UserModels() { UserGUID = Guid.NewGuid().ToString(), UserName = "张一", UserNumber = "0001", UserBirthDay = "1990.1.1", UserMail = "[email protected]", UserPhone = "13100001111", Password="aaaaaa" }; UserModels m2 = new UserModels() { UserGUID = Guid.NewGuid().ToString(), UserName = "张二", UserNumber = "0002", UserBirthDay = "1989.12.12", UserMail = "[email protected]", UserPhone = "13100002222", Password = "aaaaaa" }; UserModels m3 = new UserModels() { UserGUID = Guid.NewGuid().ToString(), UserName = "张三", UserNumber = "0003", UserBirthDay = "1989.12.12", UserMail = "[email protected]", UserPhone = "13100003333", Password = "aaaaaa" }; try { // 写数据库 context.UserContext.Add(m1); context.UserContext.Add(m2); context.UserContext.Add(m3); context.SaveChanges(); } catch (DbEntityValidationException dbEx) { } base.Seed(context); } } }
4、
方法一、在Global.asax中 添加 Database.SetInitializer<DataBaseContext>(new DatabaseInitializer());
或者:方法二:在配置文件中添加节点:
<contexts> <context type="DataBase.DataBaseContext, DataBase" disableDatabaseInitialization="false"> <databaseInitializer type="DataBase.DatabaseInitializer, DataBase" /> </context> </contexts>
时间: 2024-10-11 07:35:56