Changing the type of a property with EF Code First
2015-10-14 17:42
357 查看
The smartest way is probably to not alter types. If you need to do this, I'd suggest you to do the following steps:
Add a new column with your new type
Use Sql() to take over the data from the original column using an update statement
Remove the old column
Rename the new column
This can all be done in the same migration, the correct SQL script will be created. You can skip step 2 if you want your data to be discarded. If you want to take it over, add the appropriate statement (can also contain a switch statement).
Unfortunately Code First Migrations do not provide easier ways to accomplish this.
Here is the example code:
Add a new column with your new type
Use Sql() to take over the data from the original column using an update statement
Remove the old column
Rename the new column
This can all be done in the same migration, the correct SQL script will be created. You can skip step 2 if you want your data to be discarded. If you want to take it over, add the appropriate statement (can also contain a switch statement).
Unfortunately Code First Migrations do not provide easier ways to accomplish this.
Here is the example code:
AddColumn("dbo.People", "LocationTmp", c => c.Int(nullable: false)); Sql(@" UPDATE dbp.People SET LocationTmp = CASE Location WHEN 'London' THEN 1 WHEN 'Edinburgh' THEN 2 WHEN 'Cardiff' THEN 3 ELSE 0 END "); DropColumn("dbo.People", "Location"); RenameColumn("dbo.People", "LocationTmp", "Location");
相关文章推荐
- ShopNC B2B2C最新版去除shop方法教程
- The working copy needs to be upgraded svn: The working copy at
- Linux 性能优化工具包 sysstat 以及 sysstat 服务
- vmware中安装centos 6.7
- Android系统 adb shell命令大全
- nginx 日志访问量统计
- Working with Python subprocess - Shells, Processes, Streams, Pipes, Redirects
- eclipse移植cocos2dx工程报错:Program "bash" is not found in PATH
- 浏览器报错:Cannot read property 'style' of null
- 《王保明老师----Linux开发学习笔记》------讲12:守护进程 -----如何分析和理解下面这个创建守护进程的实例
- 学习linux必备服务器VPS
- shell的脚本循环处理
- Linux性能测试工具
- 17.1.4.4 Binary Log Options and Variables Binary Log
- keepalived实现Tomcat服务双机热备
- keepalived实现Tomcat服务双机热备
- Nginx架构的企业级应用
- ecshop添加上传图片
- linux内存释放
- linux常用命令解析