Data First or Code First Part 2

Data First or Code First Part 2

With the Code-First approach, anytime we need to make changes to the database schema, we must implement the change in the codebase first. And one way to propagate that change to the database is by invoking the following 2 commands in the command line:

First command line:

dotnet ef migrations add <migrationName> -c <dbContextName> -s <projectName>

This command will add migration script file with the name YYYYMMDDxxxxx_<migrationName> inside the <projectName> project. Illustrated below is a highlighted example where we add an email address field in the owner table:

The DateTime stamp is very helpful to determine the sequence of the change. Along with a short description, which could also be a Jira ticket #, some kind of log id, or just a simple change name this migration naming convention is an excellent way of keeping track of the changes.

And the second command line for the migration process is:

dotnet ef database update <migrationName> -c <dbContextName> -s <projectName>

This command will actually update the database schema with the changes that were scripted in the migration script from the first command line.

It is customary to have two database environments for each solution: The development environment, and the production environment. One simple way to avoid the connection string being exposed in appSettings.json, is by setting up the two connection strings in the Startup.cs. And then use a conditional statement to select one or the other, depending whether we run the program in Visual Studio debug mode or have the server run the application in publish mode. The conditional statement would look something like this:

  string connStr;


  if (this._environment.IsDevelopment())
      connStr = "devConnectionString";
  else
      connStr = "prodConnectionString";
 

And then we can use that connStr for setting up the DBContextPool like this:

services.AddDbContextPool<dbContextName>(options =>
  {
     options.UseSqlServer(connStr, b => b.MigrationsAssembly("<projectName>"));
  }
  });

Once the condition is setup and we successfully push our database migration changes to the development environment, we could comment out the if - dev block in that condition like this:

  //if (this._environment.IsDevelopment())
  //    connStr = "devConnectionString";
  //else
      connStr = "prodConnectionString";
 

And rerun the second EF command line again:

dotnet ef database update <migrationName> -c <dbContextName> -s <projectName>

This would push the schema changes to the production environment.

As expected, the migration process for Code-First approach is the exact the opposite from Data-First approach. In Data-First approach, we would make changes in the database schema first. And depending whether we use ORM or in-house generated ADO .net calls, we would regenerate the entities and then adjust the code accordingly.

Code-First approach is a newer way of implementation, and it forces the developer to take seriously any changes that require database schema changes. Any schema changes in Code-First approach must undergo a more rigorous process, versus simply adding fields in the table using a database management tool such as SSMS, PhpMyAdmin, and others.

Good series of code first approach overview.

To view or add a comment, sign in

More articles by Yogi Grantz

  • Simple .net Core Shopping Cart

    At a minimum, a shopping cart system should have these features: Showing the merchandise, adding an item to the…

    2 Comments
  • Data First or Code First

    Data First has been around for as long as I could remember. The developer would build the data structure first, such as…

  • Multi-Thread File Search Project

    Fast File Search done in C# Winform with event and +=delegate to return search result while other threads are…

  • Messaging System

    I created a WCF application to send myself emails whenever there is a problem in one of my applications. The issue can…

  • Tuning SQL Server Database

    Here is how we can optimize SQL Server Database performance. Assuming that all the Primary Keys, Foreign Keys, indexes…

  • Strategy Design Pattern

    This is just a simplified real-life sample, where we declare an object by its Interface Type (IOrder). And then pass a…

  • Interesting Facts about C# / OOPS

    1. We can declare an Interface, Abstract, or Base Class, and then instantiate any of their deriving class to create the…

  • Checking pairs of brackets

    OK so yesterday I was asked to write a code to check if the curly braces come in pairs and start with left curly first.…

  • My Interview Experience

    So I had this phone technical interview that required screen share, and I was trying to launch Skype, which I should…

  • Types of Questions

    Questions that we must have answers to - StackOverflow Question that are nice to have answers to - Google Questions…

Others also viewed

Explore content categories