第一篇:来自 .net 开发菜鸟 博主的文章:https://www.cnblogs.com/dotnet261010/p/8035213.html

第二篇:来自 JustYong 博主的文章:https://www.cnblogs.com/JustYong/p/5970683.html

感谢两位博主贡献精彩文章,感谢分享。

自己公司使用的开发框架,每次使用都要手动的把一些公共的数据添加进去,感觉很麻烦,所以就查了一下 “ 如何填充种子数据” ,看了以上的文章,很受益。

在Migrations文件夹下的 数据迁移配置文件  Configuration.cs 下的 Seed()函数中添加种子数据,代码如下,亲测针对本公司框架使用无误:

namespace ORM.Migrations
{
using Modules;
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq; public sealed class Configuration : DbMigrationsConfiguration<ORM.MyDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
ContextKey = "ORM.MyDbContext";
} protected override void Seed(ORM.MyDbContext context)
{
// This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
var pId = Guid.NewGuid();
int orderNumber = ;
var sysMenuSet = new Menu()
{
Id = pId,
ParentId = null,
Name = "系统设置",
Url = null,
IconClass = null,
OrderNumber = orderNumber
};
var menuSet = new Menu()
{
Id = Guid.NewGuid(),
ParentId = pId,
Name = "菜单设置",
Url = "Menu/Index",
IconClass = null,
OrderNumber = orderNumber
};
var dicitemSet = new Menu()
{
Id = Guid.NewGuid(),
ParentId = pId,
Name = "字典设置",
Url = "Dic/Index",
IconClass = null,
OrderNumber = orderNumber
};
var userSet = new Menu()
{
Id = Guid.NewGuid(),
ParentId = pId,
Name = "账户设置",
Url = "User/Index",
IconClass = null,
OrderNumber = orderNumber
};
var empolyeeSet = new Menu()
{
Id = Guid.NewGuid(),
ParentId = pId,
Name = "用户管理",
Url = "Employee/Index",
IconClass = null,
OrderNumber = orderNumber
};
var roleSet = new Menu()
{
Id = Guid.NewGuid(),
ParentId = pId,
Name = "角色管理",
Url = "Role/Index",
IconClass = null,
OrderNumber = orderNumber
};
var departmentSet = new Menu()
{
Id = Guid.NewGuid(),
ParentId = pId,
Name = "部门管理",
Url = "Department/Index",
IconClass = null,
OrderNumber = orderNumber
};
var tagSet = new Menu()
{
Id = Guid.NewGuid(),
ParentId = pId,
Name = "标签管理",
Url = "Tag/Index",
IconClass = null,
OrderNumber = orderNumber
};
var funcSet = new Menu()
{
Id = Guid.NewGuid(),
ParentId = pId,
Name = "权限管理",
Url = "Func/Index",
IconClass = null,
OrderNumber = orderNumber
};
context.Menus.Add(sysMenuSet);
context.Menus.Add(menuSet);
context.Menus.Add(dicitemSet);
context.Menus.Add(roleSet);
context.Menus.Add(userSet);
context.Menus.Add(empolyeeSet);
context.Menus.Add(departmentSet);
context.Menus.Add(tagSet);
context.Menus.Add(funcSet);
base.Seed(context);
}
}
}

(转载)EF 使用code first模式创建数据库和 填充种子数据的更多相关文章

  1. EntityFramework使用Code First模式创建数据库控制生成单数形式的表名

    使用Code-First模式生成数据库时,默认生成的数据库表的名称为类型的复数形式,例如实体类名称是"User",默认生成的数据库表名为“Users”,多数情况下我们并不想生成的数 ...

  2. 使用EF的Code First模式创建模型

    Entity Framework Core Entity Framework (EF) Core 是轻量化.可扩展.开源和跨平台版的常用 Entity Framework 数据访问技术. EF Cor ...

  3. 使用MVC5+Entity Framework6的Code First模式创建数据库并实现增删改查功能

    此处采用VS2017+SqlServer数据库 一.创建项目并引用dll: 1.创建一个MVC项目 2.采用Nuget安装EF6.1.3 二.创建Model 在models文件夹中,建立相应的mode ...

  4. 8天掌握EF的Code First开发系列之3 管理数据库创建,填充种子数据以及LINQ操作详解

    本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 管理数据库创建 管理数据库连接 管理数据库初始化 填充种子数据 LINQ to Entities详解 什么是LI ...

  5. Code First开发系列之管理数据库创建,填充种子数据以及LINQ操作详解

    返回<8天掌握EF的Code First开发>总目录 本篇目录 管理数据库创建 管理数据库连接 管理数据库初始化 填充种子数据 LINQ to Entities详解 什么是LINQ to ...

  6. Entity Framework应用:使用Code First模式管理数据库创建和填充种子数据

    一.管理数据库连接 1.使用配置文件管理连接之约定 在数据库上下文类中,如果我们只继承了无参数的DbContext,并且在配置文件中创建了和数据库上下文类同名的连接字符串,那么EF会使用该连接字符串自 ...

  7. EF core (code first) 通过自动迁移实现多租户数据分离 :按Schema分离数据

    前言 本文是多租户系列文章的附加操作文章,如果想查看系列中的其他文章请查看下列文章 主线文章 Asp.net core下利用EF core实现从数据实现多租户(1) Asp.net core下利用EF ...

  8. Entity Framework应用:使用EF的DataBase First模式实现数据库的增删改查

    在上一篇文章中讲解了如何生成EF的DBFirst模式,接下来讲解如何使用DBFirst模式实现数据库数据的增删改查 一.新增数据 新增一个Student,代码如下: static void Add() ...

  9. 使用Entity Framework通过code first方式创建数据库和数据表

    开发环境 WIN10 Entity Framework6.0  MVC5.0  开发工具 VS2015  SqlServer2012 1.创建上下文Context继承DbContext,并创建其他的业 ...

随机推荐

  1. poj1936 假期计划第一水

    All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29651   Accepted: 12312 Desc ...

  2. 201621123034 《Java程序设计》第9周学习总结

    作业09-集合与泛型 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 答:Map的HashMap中使用嵌套类static class Node<K,V& ...

  3. [bzoj4361] isn [树状数组+dp+容斥原理]

    题面 传送门 思路 首先,本题目的核心元素是非降子序列,而显然这个题目中的子序列只和序列的长度.位置,以及互相之间的包含关系,这些东西相关 所以我们可以依据这些先"猜"(实际上是估 ...

  4. IE6对!important单个的类是支持的

    "!important"是什么? 第一个,是设置样式的优先级,设了!important的样式的属性优先于id选择器和class选择器.,比如id为"Main"的 ...

  5. 【转】IDEA 2017破解 license server激活

    确保电脑在联网状态,在激活窗口选择license server 填入下面的license server: http://intellij.mandroid.cn/ http://idea.imsxm. ...

  6. linq使用 count与sum等

    using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; us ...

  7. nessus plugins 离线更新

    1.打开 https://plugins.nessus.org/v2/offline.php 2.申请Activation Code http://www.tenable.com/products/n ...

  8. malloc,calloc,realloc区别

    malloc:memory allocation calloc:The 'c' indicates 'cleared' realloc:The realloc() function changes t ...

  9. 几种常见的YUV格式--yuv422:yuv420【转】

    转自:http://blog.csdn.net/u012288815/article/details/51799477 关于yuv 格式 YUV 格式通常有两大类:打包(packed)格式和平面(pl ...

  10. Resin4 自定义端口

    1. Resin4配置文件发生了较大变化,分为: app-default.xml  web应用配置 cluster-default.xml  集群配置 health.xml -- 非pro版不支持 r ...