第一篇:来自 .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. cf 834 E. Ever-Hungry Krakozyabra

    cf 834 E. Ever-Hungry Krakozyabra(爆搜+数位dp) 题意: 定义一种inedible tail为一个数把每一位数字按不降的顺序排列后,去掉前导0组成的序列 比如570 ...

  2. hdu 2829 斜率DP

    思路:dp[i][x]=dp[j][x-1]+val[i]-val[j]-sum[j]*sum[i]+sum[j]*sum[j]; 其中val[i]表示1~~i是一段的权值. 然后就是普通斜率dp做法 ...

  3. vue数组对象修改触发视图更新

    直接修改数组元素是无法触发视图更新的,如 this.array[0] = { name: 'meng', age: 22 } 修改array的length也无法触发视图更新,如 this.array. ...

  4. yum 和 apt-get

    yum 和apt-get 一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debian.Ubuntu等 RedHa ...

  5. ios截屏代码[转]

    http://www.cnblogs.com/chenxiangxi/p/3547974.html 这位博主的连接中将ios自定义大小位置的截屏代码写的很不错,马上就能用的方法,对于只想马上用的程序员 ...

  6. Golang/Go语言/Go IDE/Go windows环境搭建/Go自动提示编译器/GoSublime

    Go是Google开发的一种编译型,并发型,并具有垃圾回收功能的编程语言. 罗伯特·格瑞史莫(Robert Griesemer),罗勃·派克(Rob Pike)及肯·汤普逊于2007年9月开始设计Go ...

  7. 【leetcode】500. Keyboard Row

    问题描述: Given a List of words, return the words that can be typed using letters of alphabet on only on ...

  8. 基于UDT connect连接通信以及文件传输--服务端

    网上与UDT相关的资料不多,与UDT相关的源码例子更少.最近在接触UDT,也是因为缺少相关的资料,导致学习起来甚感痛苦.下面将我自己这两天弄出来的代码贴出来,希望对在寻找相关资料的童鞋有一定的帮助.与 ...

  9. 【linux高级程序设计】(第十五章)UDP网络编程应用 3

    UDP组播通信 组播IP地址: D类IP地址  1110.**********  224.0.0.1 ~ 239.255.255.255 组播MAC地址:低23位,直接对应IP地址, 从右数第24位为 ...

  10. @Html.Raw()用法

    @Html.Raw() 方法输出带有html标签的字符串, 如:@Html.Raw("<div style='color:red'>输出字符串</div>" ...