如何首先将我的EF Code-First转换为数据库? [英] How to convert my EF Code-First to Database first?

查看:109
本文介绍了如何首先将我的EF Code-First转换为数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的第一个代码项目首先转换为数据库。是否有自动化方式,或者我应该删除实体和上下文代码,并从创建的数据库中创建一个模型?

解决方案

无法将您的代码优先类转换为数据库优先类。无论您是否存在代码优先类,都可以从数据库创建模型创建一整套新的类。



但是,您可能不想删除代码优先课程。实体框架数据库第一模型为所有实体对象创建部分类。如果您的代码优先类中有任何业务逻辑(除了普通的旧属性之外的任何东西),可以将它们声明为 partial ,删除属性,并维护业务逻辑。基本上,您在EF Designer中生成* Designer.cs文件中的属性,同时在.cs文件中定义业务逻辑。


I want to convert my code-first project to Database first. Is there an automated way or should I just delete the entities and context code and create a model from the created database?

解决方案

There is no way to convert your code-first classes into database-first classes. Creating the model from the database will create a whole new set of classes, regardless of the presence of your code-first classes.

However, you might not want to delete your code-first classes right away. The entity framework database-first model creates partial classes for all of the entity objects. If you have any business logic (anything besides the plain old properties) in your code-first classes, you can declare them as partial, remove the properties, and maintain the business logic. Essentially, you're letting EF generate the properties in the *Designer.cs file, while you define the business logic in your .cs files.

这篇关于如何首先将我的EF Code-First转换为数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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