Creating a basic MVC 6 web application with Entity Framework 7 and xUnit - Part 2

In this blog post we will connect to an actual database, create the tables using the code-first approach and add some initial data. Be aware that migrations seem to be a bit buggy at the moment, but my solution was (at least for me) the most stable one. You can clone the repository with this sample from Github.


1. Creating a database

First, open the sql server object explorer (in the View menu).

I use the default MSSQLLocalDB for this example, but you can use any running SQLServer. Now add a new database to this SQL Server.

Specify a database name and the location where to put the database file.

Finally, we need the connection string for this database. Get to the properties of the newly created database and copy the connection string.



2. Necessary changes before the migrations

Before we can run any migration, we have to set up some things. Create a Config.json in the Sample1 project and add your connection string.
Attention: you have to escape the backslashes (just replace any "\" with "\\").

Update the Startup.cs to use the Config.json file and setup the database.

There seems to be a bug(?) in Beta 4 of EF7 where the DbContext doesn't get set up properly, so we have to change our AspNetDataContext to create a connection using our connection string. As soon as this is fixed, we just have to remove the additional code from this class without having to touch anything else. The parameterless constructor is used by the migrations command and the other one when running the application. When this is fixed, I'll write another blog post about this topic.

Finally, we have to set the Id of our Player class as a key column using the [Key] attribute . Add System.ComponentModel.Annotations to the project.json in the Sample1Data project.

And add the key attribute to the Id property.


3. Running the migrations

Running the migrations is now (hopefully) a simple thing. Use the Package Manager Console and change to the Sample1 directory.

Now run the following commands:

  • dnvm upgrade
  • dnvm use 1.0.0-beta4 -r coreclr -arch x64
  • dnu restore
  • Rebuild your solution
  • dnx . ef migration add initial
  • dnx . ef migration apply

If everything worked without an error, you should now have new tables in your database. All the AspNet* tables are automatically created because we use the IdentityDbContext class as base class for our context.


4. Creating initial data

We want to insert initial data when our application starts. Since EF7 doesn't support Seeds at the moment, we have to do this on our own. Note that EF6 has the AddOrUpdate method on a DbSet which is currently missing in EF7 so we have to do the check ourselves in the InitializeData method.

And call it from the Configure method in our Startup.cs
Note: I added a missing app.UseMvc() which we need in the next chapter.

Using the ActivatorUtilities we can easily create an instance with the context we specified in ConfigureServices. If you now run the application, it will add a Player to the database if it doesn't exist yet.

Comments

Popular posts from this blog

Debugging Lua scripts in VS Code using MoonSharp

Testing the response body of middleware in ASP.NET Core