使用实体框架代码首先更新数据库模式 [英] Updating database schema with Entity Framework Code First

查看:115
本文介绍了使用实体框架代码首先更新数据库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体框架代码首先是开发新项目的一个很好的框架。但是扩展现有数据库呢?例如,如果我只想向现有数据实体添加一个附加属性,该怎么办?有没有任何代码优先的方式来做到这一点,或者我必须使用SQL Server Management Studio或类似的工具手工添加一个数据库列?



到目前为止,我发现,当架构发生变化时,如何从头重新生成整个数据库,但是我不想丢失数据。



如果没有错误,Ruby on Rails有一个工具可以生成反映新数据库更改的SQL脚本。有没有办法在EF Code First中执行此操作?

解决方案

我正在使用EntityFramework.Migrations库发展DB模式的问题。它可以创建生成SQL脚本以更新模式的迁移类。



目前它仍处于测试阶段,但我发现它很有用。不幸的是,您仍然应该生成数据迁移脚本。



您可以找到更多信息 here


Entity Framework Code First is a great framework for developing new projects. But what about extending an existing database?

For instance what if I simply want to add an additional property to an existing data entity? Is there any "code first" way to do this or will I have to add a database column by hand using SQL Server Management Studio or a similar tool?

All I have found so far is how to regenerate the entire database from scratch when the schema changes, but I don't want to lose my data.

If I'm not mistaken, there is a tool for Ruby on Rails that can generate an SQL script reflecting the new database changes. Is there any way to do this in EF Code First as well?

解决方案

I'm currently using EntityFramework.Migrations library which addresses the problem of evolving the DB schema. It works creating migration classes which generate SQL scripts to update your schema.

It is still in beta at this time but I found it useful. Unfortunatly you should still generate data migration scripts.

You can find more information here.

这篇关于使用实体框架代码首先更新数据库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆