Switch from Entity Framework Database First to Code First
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:
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.
链接地址: http://www.djcxy.com/p/90218.html上一篇: 先从现有数据库中获得代码
下一篇: 从实体框架数据库优先切换到代码优先