Adventures on the edge

Learning new development technologies
    "When living on the bleeding edge - you sometimes have to bleed" -BillKrat

Code first with Entity Framework Core (desktop app)

Source: OPEN SOURCE CODE

NuGet Prerequisites:

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.EntityFrameworkCore.SqlServer (for SQL Server usage)

First you’ll want to create your data context, you’ll see that mine resides in the data access layer project for my Contact Manager application; which follows best practices and patterns, but makes EF complain (video has a work-around).  My context uses POCO objects from my Model project which has no references to Entity Framework or any other external libraries.

Next create the database tables by launching the NuGet Package Manager console and typing in:

  1. add-Migration <YourContext>
  2. update-database

Your done!   The following video demonstrates how I had a failing unit test because three were no tables in my database.  With the above two steps the tables were generated.

In figure 1 we show the POCO class that generated the tables, and relationships, shown in figure 2.

Video 1 fixing failing unit test because there are tables in the database

ContactTable

Figure 1 Contact Class

Schema

Figure 2 Schema generated from Contact Class

Context

Figure 3 Context code – referencing only POCO objects

Comments are closed