【Code First】 Data Seed
来源:互联网 发布:长板淘宝 编辑:程序博客网 时间:2024/04/28 16:39
File Structure:
Code Snippet:
public class TESDbContext : DbContext { public TESDbContext() : base("TESDatabse") { // 设置自己的初始化器 Database.SetInitializer<TESDbContext>(new TESDatabaseInitializer()); } public DbSet<TaskScope> TaskScopes { get; set; } public DbSet<TaskStatus> TaskStatus { get; set; } public DbSet<TESUser> TESUsers { get; set; } public DbSet<Team> Teams { get; set; } } public class TESDatabaseInitializer : IDatabaseInitializer<TESDbContext> { public void InitializeDatabase(TESDbContext context) { if (!context.Database.Exists()) { context.Database.Create(); this.Seed(context); } } // Initialize stored proc and build-in users. protected virtual void Seed(TESDbContext context) { Team se = new Team(); se.TeamName = "ForAddingBuiltInAdmins"; se = context.Teams.Add(se); context.SaveChanges(); string builtInUsers = ConfigurationManager.AppSettings["BuiltInAdmins"]; if (string.IsNullOrEmpty(builtInUsers)) { throw new InvalidDataException("Built-in admin user is not specified.<br/>Please add the following node to the appSetting section of your web.config:<br/> <add key=\"BuiltInAdmins\" value=\"v-elzhu;v-junweh\"/>"); } foreach (var user in builtInUsers.Split(';')) { TESUser model = new TESUser(); model.Alias = user; model.DisplayName = user; model.Roles = 1; model.RoleNames = "SystemAdmin"; model.TeamId = se.Id; context.TESUsers.Add(model); } context.SaveChanges(); this.AddStortedProcs(context); } private string[] Resources = new string[] { "sp_getGroupReport.sql", "sp_getIndividualReport.sql", "sp_getWeeklyTrendReport.sql" }; private void AddStortedProcs(TESDbContext context) { string rootFolder = "Wicresoft.MSStore.TES.Data.EntityFramework.SQLScript."; Assembly asm = Assembly.GetAssembly(typeof(TESDbContext)); try { foreach (var item in Resources) { using (StreamReader sr = new StreamReader(asm.GetManifestResourceStream(rootFolder + item))) { string sp_getGroupReport = sr.ReadToEnd(); context.Database.ExecuteSqlCommand(sp_getGroupReport); } } } catch (Exception ex) { if (context.Database.Exists()) { context.Database.Delete(); } throw new Exception("Filed to create necessary stored procedures.ERROR:" + ex.Message); } } }
- 【Code First】 Data Seed
- Database Initialization Strategies in Code-First & Seed Data(sample role)
- Model First, Database First or Code First–Update to Data Points Column
- Generic Data Access Helper using Entity Framework 6.1 (Code First)
- Entity Framework Code First (三)Data Annotations
- Code First
- code first
- code first
- First Code
- SEED
- seed()
- EF Code First 学习笔记:约定配置Data Annotations+Fluent API
- Code First for Mysql 错误:未为提供程序“MySql.Data.MySqlClient”找到任何 MigrationSqlGenerator
- Entity Framework Code First (七)空间数据类型 Spatial Data Types
- Entity Framework Code First属性映射约定(Data Annotation基础篇)
- Code First DataAnnotations
- Code First一些问题
- Node(2) First Code
- JPEG编码
- Android中绘制图表的开源框架AChartEngine初识
- NVSim
- 进程与线程的区别联系
- tesseract-ocr3.02字符识别过程操作步骤
- 【Code First】 Data Seed
- iptables 添加,删除,查看,修改
- Inflate用法!!!i
- AutoHotkey入门教程
- 分布式—事务
- 交换机与路由器的区别
- 调用约定的认识
- 使用Spread Studio for .NET如何移动工作表
- C++ Tr1中的正则表达式