New Model Using Entity Framework 7 and ASP.NET 5

Last week I created a very basic ASP.NET 5 application based on a Visual Studio template with no changes. I am now going to take that project and add in the ability to do CRUD operations for the following Contact model.

I am using Entity Framework 7 to manage database interaction. Entity Framework 7 is a full rewrite just like ASP.NET 5. To get access to contacts a DbContext is need just as in EF 6. The OnConfiguring override is new in EF 7 and was needed for me to get the controller generation process to work which is covered below. Optimally this connection string would be in a configuration file, but that is out of the scope of this post.

At this point I was ready to add a migration for the new model. I ran into a few issues getting this process working. I was unable to get migrations to work using the package manager console and ended up having to use a console. This should all be smoothed out by the time final release is done.

I ran all the command while in the same folder as the project.json file. The commands are run by the .Net Execution Environment or as the exe is name dnx.  Below is a screenshot of the console after running the migration add command.

cmderEfAddMigration

The next step was to apply the migration to get the database setup which is also run in the console.

ASP.NET 4 had great support for creating controllers and views from a model. That functionality is currently missing from the UI of Visual Studio. This is another thing that I am sure will be added by the final release. But for now back to the console with the following command to create a controller and associated views.

Now we need some way to access the new views. I added a Contacts item to the nav bar. This is done by editing _Layout.cshtml in Views/Shared folder and adding the following line to the ul with a class of “nav navbar-nav”.

At this point contacts should be useable.  What I discovered when I clicked on my Contacts link was a useable by very ugly page. The generated views are not using the shared layout by default. To fix this open all the views and remove the following from the top of the page.

And the following from the bottom.

With the above removed the views will now use _Layout.cshtml and match the rest of the site. At this point the generated items pretty much match the scaffolding option from ASP.NET 4.

2 thoughts on “New Model Using Entity Framework 7 and ASP.NET 5”

  1. Can you please upload the full Project please?

    By the way:
    in the Code above, there is a missing “t” Letter:
    wrong: dnx . ef migration add ContactsInitial -c ContactsDbConext
    right: dnx . ef migration add ContactsInitial -c ContactsDbContext

    1. Thank you for pointing out my missing letter the post has been updated.

      I don’t that this project to upload anymore. You can check out this project on GitHub. I build it to compare Angular 2 and Aurelia, but it also contains razor views build off a model that is similar to the model in this post. Please let me know if you have further questions.

Leave a Reply

Your email address will not be published. Required fields are marked *