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.

public class Contact
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    [Display(Name = "Zip Code")]
    public string ZipCode { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }


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.

public class ContactsDbContext : DbContext
    public DbSet Contacts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;" +
                                    "Database=aspnet5-Contacts;" +
                                    "Trusted_Connection=True;" +

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.


dnx . ef migration add ContactsInitial -c ContactsDbContext

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

dnx . ef migration appy ContactsInitial -c ContactsDbContext

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.

dnx . gen controller -name ContactsController --dataContext ContactsDbContext --model Contact

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”.

<li><a asp-controller="Contacts" asp-action="Index">Contacts</a></li>

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.

    Layout = null;

<!DOCTYPE html>

    <meta name="viewport" content="width=device-width" />

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 Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.