Monday, February 5, 2018

Code First Approach

This is one of the three approaches what Entity Framework provides to create an entity model and work with it. In this model of work flow, it supports :

  • This approach targets a database which doesn't exist yet and Code First will create it
  • Can be used against a blank database, means database exists but no tables are in place to use within the database
  • Additional needed configurations can be performed either using Data Annotations, Properties or with the help of Fluent API








Code First approach is recommended when:
  • Model classes contain logic and contain non-standard structures
  • Model is divided between many assemblies that are not all known at design time
  • Databases are short lived and can often change
  • When one wants to be able to write and quickly and easily run integration tests on LocalDB
  • If you rather see the model and the mappings in a single place instead of going over diagrams and mapping windows
Advantages:
  • One common syntax like LINQ for all object queries whether it is database or not
  • Pretty fast if used as intended and easy to implement Separation of Concerns
  • Less coding required to accomplish complex tasks
  • It's fast and straight forward using LINQ objects for all CRUD operations
  • Easy to map business objects
  • Keeps good performance when you work with a small / middle domain model
Disadvantages:
  • You have to think in a non traditional way of handling data, not available for every database
  • If there is any schema change in database, you have to update the schema and solution as well
  • It's limited when you work with a huge domain model
Steps to implement Code First approach
  • Create a new ASP.NET MVC or Web API project
  • Using "Manage Nuget Packages" option install latest Entity Framework
  • Update web.config with connection string
    <connectionStrings>
    <add name="CodeFirstApproachContext" connectionString="data source=DESKTOP-BSV3G59\SQLEXPRESS;Initial Catalog=CodeFirstApproach; Integrated Security=SSPI;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
  • Create new Context class (best practice is to have the same as connection string name) by deriving it from DbContext. This will contain the OnModelCreating override along with DbSet<> definitions
  • Start adding your POCO objects to Model folder as you need
  • Launch Package Manager Console and execute below commands in the same order to generate the required files
    • enable-migrations This will create Migrations folder and Configuration.cs file with settings
    • add-migration "Name of the migration" This will create the migration file of provided name with changes to run
    • update-database -verbose This command will run the migrations created as part of above command to update the database with identified changes
  • After this step, we can check the database by launching SSMS to see the created tables as updated by update-database command above
  • All DbSet<> will be created and can be accessed using context object
  • From now on, any kind of changes to the POCO objects needs to be taken care by using add-migration and update-database -verbose commands

No comments:

Post a Comment