从实体框架数据库首先切换到代码优先 [英] Switch from Entity Framework Database First to Code First

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

问题描述

我们的解决方案目前基于实体框架数据库。我们有一个T4模板从EDMX生成存储库类。



我们正在审查我们发布更改的计划方法,特别是数据库更改。如果我们先继续使用数据库,那么我们将需要单独生成脚本来更改开发和其他数据库。



似乎使用Code First,我们只需更改模型,并生成脚本来更改各种数据库。这似乎更直接,不涉及手工制作脚本流程和降低风险。



所以,如果我们进行切换,它只是一个例子:




  • 将以前生成的模型从我们的实体
    项目中的EDMX移动到(他们现在都在一个类文件中)到(最好是
    单独)实体项目中的文件夹中的文件

  • 调整T4模板从新位置获取模型

  • 不再使用EDMX和从数据库更新

  • 当我们想改变模型时,只需更改(以前但不再生成的)类

  • 使用代码优先迁移来实现对测试和
    其他数据库的更改



最后如何我们会看到模型之间的关系吗?有没有办法创建图?



谢谢,



Chris

解决方案

这是我过去所做的一切。随着时间的推移,我尝试了许多不同的方法目前,我正在使用 EF Reverse Poco Generator 来允许对数据库进行更改并反映在代码中,并初始化生成Model / Poco类。我也可以手动更改现有的类。然后我为每个更改生成迁移。



代码优先模型允许定义与现在相同的导航属性,因此您可以通过代码查看关系以及用于可视化类关系的工具。



如果要查看数据库结构,可以在MS Sql中使用数据库图功能,如果这是您正在使用的。我的印象是,鼓励您使用实体框架本身以外的工具来可视化数据库或类关系。这允许EF团队专注于数据库代码,而不是与VS进行复杂的UI集成。



我个人依靠数据库图来检查我的类结构和他们输出的数据库但是,我觉得自己只是看Poco课程。我没有找到任何特殊的类图表工具。



所有这一切,你的陈述是正确的。虽然我将从EF Reverse Poco开始,从现有的数据库生成类,为您提供更多的灵活性。使用反射而不是使用XML解析(请参阅T4 Toolbox进行输出文件管理),使您的T4在这些类中得到启发。



反映在程序集上是一个粘滞你开始了您需要确保您的EF / Poco汇编已编译,以便它可以加载到内存中并由您的T4反映出来。在极少数情况下,根据加载程序集的方式,可以停止刷新程序集,因此必须重新启动VS。我每个月碰到这几次,所以这不是我的交易破坏。一旦我得到它和运行它是有道理的。


Our Solution is currently based on Entity Framework Database First. We have a T4 Template that generates repository classes from the EDMX.

We are reviewing our planned approach for releasing changes, especially Database changes. If we continue with Database first, then we will need to separately generate scripts to change the development and other databases.

It seems that with Code First, we simply change the model and that generates scripts to change the various databases. This seems more straightforward, does not involve hand crafting scripting processes and lower risk.

So, if we make the switch, is it simply a case of:

  • Moving the previously generated models from EDMX in our Entities Project to (they're all currently in one Class File) to (preferably separate) Class Files in a folder within the Entities Project
  • Adjust the T4 Template to pick up the models from their new location
  • No longer using the EDMX and Update from Database
  • When we want to make a change to the model, simply changing the (previously but no longer generated) classes
  • Using Code First Migrations to implement changes to the Test and other databases

Finally how would we see the relationships between the models? Is there a way of creating the diagram?

Thanks,

Chris

解决方案

This is something I have done in the past. Over time I have tried many different methods. Currently I am using EF Reverse Poco Generator to allow changes to be made to the database and reflected in code and to initially generate the Model/Poco classes. I can also make changes to the existing classes manually. Then I generate migrations for each change.

The code-first model allows for the defining of navigation properties the same as you have now, so you can see the relationships through code and tools for visualizing class relationships.

If you want to see the database structure you can use the 'Database Diagrams' feature in MS Sql if that is what you are using. It is my impression that you are encouraged to use tools other than entity framework itself for visualizing database or class relationships. This allows the EF team to focus on Database code instead of complex UI integration with VS.

I personally I depend on Database Diagrams to check my class structure and the DB they output, but I find it natural to just look at the Poco classes. I haven't found any exceptional class diagramming tools.

All that being said, you are correct in your statements. Although I would start from the EF Reverse Poco generated classes from your existing db to give you that added flexibility. Point your T4 at those classes using reflection instead of XML parsing (Look at T4 Toolbox for output file management) to get you started.

Reflecting on assemblies is a sticky bit when you get started. You need to make sure you have the EF/Poco assembly compiled so it can be loaded in memory and reflected on by your T4. On rare occasions, depending on how you are loading the assembly, it can stop refreshing the assembly so you have to restart VS. I run across this a couple times a month, so it wasn't a deal breaker for me. Once I got it up and running it made sense.

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

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