Entity Framework with .Net Core


.net coreI have been using Entity Framework for a few months now and I thought it was time that I start putting up a few tutorials about it. So here is the first one. In this tutorial we are going to make a simple console application to hold employee data.

Setup

Create a .net core 2.0 console application and add the following Nuget packages

Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools

Define the model

As I said this will be our employee database. The first thing we are going to have is a class to old our employees.


namespace EntityFrameworkCore.Model
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Job Job { get; set; }
    }
}

Now to make things interesting I have also added a model for a job type.


namespace EntityFrameworkCore.Model
{
    public class Job
    {
        public int Id { get; set; }
        public string Title { get; set; }
    }
}

All this is every nice but these are just classes. Some how we need to get this inserted into a database somehow.
[wp_ad_camp_3]

Create Datacontext

The DB context file is where we define our database.

DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database. DbContext is the primary class that is responsible for interacting with the database.


namespace EntityFrameworkCore.Data
{
    public class EmployeeContext : DbContext
    {
        public DbSet Employees { get; set; }
        public DbSet Jobs { get; set; }
        public DbSet Locations { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("server=.\\sqlexpress;initial catalog=EntityFrameworkEmployee;Integrated Security=true");
        }
    }
}

Here I have added a DbSet for each of my classes. Notice I also have added location we are going to use that in the next tutorial.

A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext.Set method. Namespace: System.Data.Entity.

Initializing the database

      Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created
      Update-Database will apply any pending migrations to the database

The first thing we need to do is to create the migration step needed to initialize our database. To do that you run the following command in the Console.
[wp_ad_camp_5]

Add-Migration InitialDatabase

Once that is done you can run

Update-database

Your database should now be created. Note there is no data in it but we have created our database.

Conclusion

In this tutorial we looked at how to create our model in a .Net Core console application and turn them into tables in our database. In the next tutorial we are going to look at how to add data to the database. As well as how to select it out of the database.


About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

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.