Whether to Code First or Design Schema first? RRS feed

  • Question

  • I am new to EF and just installed 4.1 version. People recommend Code First style with the new EF, but how to design classes when you don't have a basic idea of database schema?

    What are the prerequisites for Code First style and what considerations to take in mind to proceed in the correct way.


    Friday, July 22, 2011 4:11 PM


  • Hi,

    If you should use Code-First or Model-First is a matter of taste actually. Each of them does not give anything more functionality than the other, it's more the matter of taste of how you like to have your code.

    If you don't know your database layout, code-first may be an idea to use since you then first can define your classes, which properties, navigation properties etc. they should have. When you have a code structure that suites your needs, you can apply them to a DbContext using DbSets and then let the code generate the database model for you. However! The current release of Code-First does not merge database changes. It recreates the database which may be a problem with incremental changes to your classes, so it is good to really get everything you need from the beginning. With Model-First there is extensions to the database schema generator that can help you with upgrading existing databases instead of recreating them.

    I hope this cleared up a but, but again it all ends with how you like to have your code.

    Sunday, July 24, 2011 12:00 PM