Asp.net Mvc Entity Framework Code First 数据库迁移
1、创建Mvc项目
2、安装Entity Framework
2.1、如下图打开程序包管理器控制台:

2.2、输入命令Install-Package EntityFramework,即可安装EntityFramework,如下图:

3、创建你的需要的实体类
我这里简单创建一个Student类,如下:
public class Student
{
public int ID { set; get; }
public string Name { set; get; }
}
4、创建数据库上下文类(Database context)
新建类(我这里用SchoolContext命名),继承DbContext,如下:
.....
using System.Data.Entity.ModelConfiguration.Conventions;
using WebApplication1.Models;
......
public class SchoolContext: DbContext
{
public SchoolContext() : base("SchoolContext") { } public DbSet<Student> Student { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//prevents table names from being pluralized(防止生成的数据库表名称是复数形式,如Students)
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
public DbSet<Student> Student { get; set; }这句不能忘了加,这对应刚才建的Student类。
5、修改配置文件(Web.config)
在Web.config文件里添加代码:
<connectionStrings>
<add name="SchoolContext" connectionString="data source=.;initial catalog=SchoolDB;persist security info=True;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
6、在程序包管理器控制台执行迁移命令
6.1、输入enable-migrations,并执行,项目里会自动添加Migrations文件夹,并生成类文件Configuration.cs,如下:
internal sealed class Configuration : DbMigrationsConfiguration<WebApplication1.DAL.SchoolContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
} protected override void Seed(WebApplication1.DAL.SchoolContext 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. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
6.2、输入命令:add-migration "第一次迁移"
后面的汉字可以自定义。
项目会生成201611010701013_第一次迁移.cs文件,如下:
public partial class 第一次迁移 : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Student",
c => new
{
ID = c.Int(nullable: false, identity: true),
Name = c.String(),
})
.PrimaryKey(t => t.ID); } public override void Down()
{
DropTable("dbo.Student");
}
}
6.3、输入命令update-database即可在数据库看到数据库了。
参考文档:https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
Asp.net Mvc Entity Framework Code First 数据库迁移的更多相关文章
- ASP.NET MVC+Entity Framework code first 迁移
再来一张,选择 MVC 模版,其他的没选过,不会用 =_=!! 身份验证用个人用户账户,这个是为了偷懒,话说 ASP.NET Identity 还是很给力的,不用白不用 ^_^~ 点击确定之后,会看 ...
- ASP.NET MVC 4下 Code First 数据库迁移
一.命令开启 1.打开控制台:视图->其他窗口->程序包管理器控制台: 2.启动数据库迁移,执行命令:enable-migrations 创建成功后会新增migrations目录等. 若 ...
- ASP.NET MVC - Entity Framework
ASP.NET MVC - Entity Framework 实体关系 关系是某个实体(表)的一条记录对应于另一个实体(表)的一条或多条记录. 一对多关系 单方面的包含关系称为一对多,而一对多和一对一 ...
- 使用ASP.NET MVC+Entity Framework快速搭建系统
详细资料: http://www.cnblogs.com/dingfangbo/p/5771741.html 学习 ASP.NET MVC 也有一段时间了,打算弄个小程序练练手,做为学习过程中的记录和 ...
- 使用ASP.NET MVC+Entity Framework快速搭建博客系统
学习 ASP.NET MVC 也有一段时间了,打算弄个小程序练练手,做为学习过程中的记录和分享. 首先,得确定需求,木有需求的话,那还搞个毛线呀!嗯……大致思考了一下,终于得出如下需求: 1.能自定义 ...
- Entity Framework Code First 数据迁移
需要在[工具 --> NuGet 程序包管理器 --> 程序包管理器控制台]中输入三个命令: Enable-Migrations (初次迁移时使用) Add-Migration [为本次迁 ...
- ASP.NET MVC+Entity Framework 访问数据库
Entity Framework 4.1支持代码优先(code first)编程模式:即可以先创建模型类,然后通过配置在EF4.1下动态生成数据库. 下面演示两种情形: 1.代码优先模式下,asp.n ...
- ASP.NET MVC+Entity Framework 4.1访问数据库
Entity Framework 4.1支持代码优先(code first)编程模式:即可以先创建模型类,然后通过配置在EF4.1下动态生成数据库. 下面演示两种情形: 1.代码优先模式下,asp.n ...
- Entity Framework(code first)数据库初始化
//1.修改模型,重设数据库 using System.Data.Entity; Database.SetInitializer<LisknoveDataContext>(newDropC ...
随机推荐
- 【Cocos2d-x for WP8 学习整理】(2)Cocos2d-Html5 游戏 《Fruit Attack》 WP8移植版 开源
这一阵花了些时间,把 cocos2d-html5 里的sample 游戏<Fruit Attack>给移植到了WP8上来,目前已经实现了基本的功能,但是还有几个已知的bug,比如WP8只支 ...
- SQL SERVER中的扩展属性
以前在SQL SERVER建表时,总看到扩展属性,但一直未使用过.今天研究下: 增加扩展属性: 语法: sp_addextendedproperty [ @name = ] { 'property_n ...
- NPOIExcel
public class NPOIExcel { private string _title; private string _sheetName; private string _filePath; ...
- bootstrap 使用需注意的一些点
table 中td的宽度可以td 的style设置,然后在设置内部比如img对象款高实现对于 table其中某列的设置.
- linux 查找文件或者内容常用命令
whereis <程序名称> find [路径] <表达式> locate <文件名称> 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符 ...
- LogPolar 对数极坐标
LogPolar 对数极坐标 cvLogPolar 对数极坐标(logpolar)是仿真生物视网膜中央凹陷的特性,具有数据压缩的能力,可用于目标跟踪中快速尺度和旋转变换不变的模板匹配. 对数极坐标其实 ...
- [转] valuestack,stackContext,ActionContext.之间的关系
三者之间的关系如下图所示: ActionContext 一次Action调用都会创建一个ActionContext 调用:ActionContext context = ActionContext ...
- 免费领取百度云盘2048G永久空间,永久离线下载特权
百度云盘和360云盘之间的竞争可谓争锋相对,前段时间,百度和360就网盘免费都采取了自己的措施,最终,360一锤定音,直接免费送36T,并且如果你超过了36T的容量,还可以自动免费扩容! 可参看文章: ...
- 二、jquery选择器
在jquery库中,可以通过选择器实现DOM元素快捷选择这一重要的核心功能. 1.选择器的优势 (1)代码更简单 由于在jquery库中,封装了大量可以通过选择器直接调用的方法或函数,使编写代码更加简 ...
- 加速下载gradle
http://www.jianshu.com/p/e887203e30f6 另外idea runconfiguration里边 gradle project要选项目根目录,而不是build脚本.