EntityFramework CodeFirst
2015-12-07 22:13
417 查看
1.新建一个类库
2.新建一个上下文类 DataBaseContext 继承 DbContext ,在上下文中的构造函数中 public DataBaseContext()
: base("name=DataBase"){} 设置数据库名
3.新建一个User模型类,在上下文中绑定 public DbSet<User> Users { get; set; }
4.工具-NuGet程序包管理器-程序包管理器控制台,Enable-Migrations 启用迁移,新建app.config,加入connectionStrings节点,连接数据库,我这里是sqlserver
<connectionStrings>
<add name="DataBase" connectionString="Data Source=.;Initial Catalog=WebAPI;User ID=sa;Password=sasa" providerName="System.Data.SqlClient" />
</connectionStrings>
5.在程序包管理器控制台中添加迁移记录,add-migration 20151207(这是版本号),update-database更新到数据库,至此完成
6.版本迁移:比如本地有1、2、3、4个版本,服务器上只到1这个版本,现在需要把2到4版本的所有内容更新到服务器数据库中,使用下面的语句
Update-Database -Script -SourceMigration:2 -TargetMigration:4
会生成2到4的所有需要更新的内容,复制到数据库中执行。
2.新建一个上下文类 DataBaseContext 继承 DbContext ,在上下文中的构造函数中 public DataBaseContext()
: base("name=DataBase"){} 设置数据库名
3.新建一个User模型类,在上下文中绑定 public DbSet<User> Users { get; set; }
4.工具-NuGet程序包管理器-程序包管理器控制台,Enable-Migrations 启用迁移,新建app.config,加入connectionStrings节点,连接数据库,我这里是sqlserver
<connectionStrings>
<add name="DataBase" connectionString="Data Source=.;Initial Catalog=WebAPI;User ID=sa;Password=sasa" providerName="System.Data.SqlClient" />
</connectionStrings>
5.在程序包管理器控制台中添加迁移记录,add-migration 20151207(这是版本号),update-database更新到数据库,至此完成
6.版本迁移:比如本地有1、2、3、4个版本,服务器上只到1这个版本,现在需要把2到4版本的所有内容更新到服务器数据库中,使用下面的语句
Update-Database -Script -SourceMigration:2 -TargetMigration:4
会生成2到4的所有需要更新的内容,复制到数据库中执行。
相关文章推荐
- 51nod 1288:汽油补给
- php字符串分割
- 在cooja上使用collectview(二)
- find 命令的参数详解
- caja 原理 : 前端
- 第三个Sprint冲刺第三天
- zabbix监控报错zabbix server is not running: the information displayed may not be current的解决
- 美团推荐系统整体框架与关键工作
- 响应式图片 <img src=,srcset=,sizes=>
- iOS:App上架流程和支付宝支付流程
- 文件读写
- WordPress获取特色图像的链接地址
- 网页生成图片快照
- 抽象类和接口
- Palindrome Linked List
- Android Third Party Libraries and SDK's
- iOS 获取设备的UUID
- mv命令
- 【Objective-C学习记录】第六天
- Project Euler 99:Largest exponential 最大的幂