Monday, February 5, 2018

Model First Approach

This is another way besides Code First and Database First approaches to create and work with your models using Entity Framework. If supports

  • "Draw" your model and lets workflow generate the database and POCO classes
  • Model is stored in EDMX and can be viewed and edited using EF Designer
  • The classes that you interact with in your application are generated from the EDMX file



Model First approach is recommended when:
  • Is useful when starting a new project where the database doesn't event exist
  • One don't want to write neither code not SQL
  • Draw the Model using designer and workflow will generate everything you need
Advantages
  • Will be able to create the Database schema and the class diagram as a whole using the EF designer, which is great when the data structure is big
  • Whenever there is a Database change, the Model can be updated without data loss
Disadvantages
  • The diagram driven, autogenerated SQL scripts can lead to data loss in case of updates. So, as a workaround the changes need to be manual instead.
  • Dealing with the diagram can be tricky, especially if we want to have control over the Model classes
Steps to implement Model First approach
  • Create a new ASP.NET MVC or Web API project
  • Using Manage Nuget Packages option install latest Entity Framework
  • Add New ADO.NET Entity Data Model item to the project by selecting "Empty EF Designer Model" option
  • Right click and add New Entity on EDMX designer
  • Once all required Entities are created, Database can be generated by right clicking and selecting "Generate Database from Model" option
  • Now, generate T4 templates using "Add Code Generation Item" from right clicking on EDMX designer

No comments:

Post a Comment