dotConnect for MySQL Documentation
In This Topic
    Entity Framework Core Code-First Tutorial for .NET Core
    In This Topic

    This tutorial guides you through the process of creating a simple application powered by Entity Framework Core. This application will create tables in the database based on the model in run-time, fill them with sample data, and execute queries.

    This tutorial is for .NET Core. For Full .NET Framework see Entity Framework Core Code-First Tutorial for Full .NET Framework.

    Requirements

    If you want to target Entity Framework Core 3.1, this tutorial requires the following:

    If you want to target Entity Framework Core 2.2, this tutorial requires the following:

    If you want to target Entity Framework Core 1.1, this tutorial requires the following:

    Note that Entity Framework support is available only in Professional and Developer Editions of dotConnect for MySQL.

    Creating New Project

    1. Open Visual Studio
    2. On the File menu point to New and then click Project. The New Project dialog box will open.
    3. On the left side of the dialog box select Installed -> Visual C# -> .NET Core.
    4. On the right side of the dialog box select Console App.
    5. Enter the project name and location if necessary.
    6. Click OK. A new project will be created.

    Installing NuGet Packages

    After we created a new project, we need to add the necessary NuGet packages to it.

    1. On the Tools menu point to NuGet Package Manager and then click Package Manager Console.
    2. Run the following command in the Package Manager Console:
      Install-Package Devart.Data.MySql.EFCore

    For Entity Framework Core 2, you need to perform the following actions instead:

    1. On the Tools menu point to NuGet Package Manager and then click Package Manager Console.
    2. Run the following command in the Package Manager Console:
      Install-Package Devart.Data.MySql
    3. Run the following command in the Package Manager Console:
      Install-Package Microsoft.EntityFrameworkCore.Relational -Version 2.2.6
    4. Add the Devart.Data.MySql.Entity.EFCore.dll assembly from the \Entity\EFCore2\netstandard2.0\ subfolder of the dotConnect for MySQL installation folder to your project.

    Sample Application Code

    To create the sample application, let's perform the following steps:
    1. Create a DbContext descendant.
      using Microsoft.EntityFrameworkCore;
      using System;
      using System.Collections.Generic;
      using System.ComponentModel.DataAnnotations;
      using System.ComponentModel.DataAnnotations.Schema;
      using System.Data;
      using System.IO;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
       
      public class MyDbContext : DbContext {
       
      }
      Imports Microsoft.EntityFrameworkCore
      Imports System.Collections.Generic
      Imports System.ComponentModel.DataAnnotations
      Imports System.ComponentModel.DataAnnotations.Schema
      Imports System.Data
      Imports System.IO
      Imports System.Linq
      Imports System.Text
      Imports System.Threading.Tasks
      
      Public Class MyDbContext
      	Inherits DbContext
      
      End Class
      
    2. Register Entity Framework Core provider for using with our DbContext and specify the connection string. For this override the OnConfiguring method.
      public class MyDbContext : DbContext {
       
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
      
           optionsBuilder.UseMySql(@"User Id=root;Host=localhost;Database=Test;");
        } 
      }
      Public Class MyDbContext
      	Inherits DbContext
      
      	Protected Overrides Sub OnConfiguring(optionsBuilder As DbContextOptionsBuilder)
      
      		optionsBuilder.UseMySql("User Id=root;Host=localhost;Database=Test;")
      	End Sub
      End Class
      
    3. Create entity classes, used in the model. If necessary, set DataAnnotation attributes for the classes and properties.

      + Show/Hide code

    4. Add our classes to the DbContext descendant as DbSet properties. If necessary, you can also write fluent mapping, by overriding the OnModelCreating method.
      public class MyDbContext : DbContext {
       
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
       
          optionsBuilder.UseMySql(@"User Id=root;Host=localhost;Database=Test;");
        }
       
        protected override void OnModelCreating(ModelBuilder modelBuilder) {
       
          modelBuilder.Entity<OrderDetail>()
            .HasKey(p => new { p.OrderID, p.ProductID });
        } 
        
        public DbSet<Product> Products { get; set; }
        public DbSet<ProductCategory> ProductCategories { get; set; }
        public DbSet<OrderDetail> OrderDetails { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<Company> Companies { get; set; }
        public DbSet<PersonContact> PersonContacts { get; set; }
      }
      
      Public Class MyDbContext
      	Inherits DbContext
      
      	Protected Overrides Sub OnConfiguring(optionsBuilder As DbContextOptionsBuilder)
      
      		optionsBuilder.UseMySql("User Id=root;Host=localhost;Database=Test;")
      	End Sub
      
      	Protected Overrides Sub OnModelCreating(modelBuilder As ModelBuilder)
      
      		modelBuilder.Entity(Of OrderDetail)().HasKey(Function(p) New From { _
      			p.OrderID, _
      			p.ProductID _
      		})
      	End Sub
      
      	Public Property Products() As DbSet(Of Product)
      		Get
      			Return m_Products
      		End Get
      		Set
      			m_Products = Value
      		End Set
      	End Property
      	Private m_Products As DbSet(Of Product)
      	Public Property ProductCategories() As DbSet(Of ProductCategory)
      		Get
      			Return m_ProductCategories
      		End Get
      		Set
      			m_ProductCategories = Value
      		End Set
      	End Property
      	Private m_ProductCategories As DbSet(Of ProductCategory)
      	Public Property OrderDetails() As DbSet(Of OrderDetail)
      		Get
      			Return m_OrderDetails
      		End Get
      		Set
      			m_OrderDetails = Value
      		End Set
      	End Property
      	Private m_OrderDetails As DbSet(Of OrderDetail)
      	Public Property Orders() As DbSet(Of Order)
      		Get
      			Return m_Orders
      		End Get
      		Set
      			m_Orders = Value
      		End Set
      	End Property
      	Private m_Orders As DbSet(Of Order)
      	Public Property Companies() As DbSet(Of Company)
      		Get
      			Return m_Companies
      		End Get
      		Set
      			m_Companies = Value
      		End Set
      	End Property
      	Private m_Companies As DbSet(Of Company)
      	Public Property PersonContacts() As DbSet(Of PersonContact)
      		Get
      			Return m_PersonContacts
      		End Get
      		Set
      			m_PersonContacts = Value
      		End Set
      	End Property
      	Private m_PersonContacts As DbSet(Of PersonContact)
      End Class
      
      

    5. Now let's choose how to create the database. We can generate Code-First Migrations. Or, for test purposes, we can implement the analogue of Entity Framework 6 initialization strategy DropCreateDatabaseAlways.

      + Show/Hide code

    6. Now let's add code that creates the context, re-creates the database, fills it with the test data, and executes LINQ to Entities queries. Before compiling the following code add the Microsoft.EntityFrameworkCore namespace to the using list (for C#) or to the Imports list (for VB).
        class Program {
       
          static void Main(string[] args) {
       
            var context = new MyDbContext ();
       
            Console.WriteLine("Entity Framework Core Code-First sample");
            Console.WriteLine();
       
            MyDbContextSeeder.Seed(context);
       
            Console.WriteLine("Products with categories");
            Console.WriteLine();
       
            var query = context.Products.Include(p => p.Category)
                .Where(p => p.Price > 20.0)
                .ToList();
       
            Console.WriteLine("{0,-10} | {1,-50} | {2}", "ProductID", "ProductName", "CategoryName");
            Console.WriteLine();
            foreach (var product in query )
              Console.WriteLine("{0,-10} | {1,-50} | {2}", product.ProductID, product.ProductName, product.Category.CategoryName);
       
            Console.ReadKey();
          }
        }
      
      Class Program
      
      	Private Shared Sub Main(args As String())
      
      		Dim context = New MyDbContext()
      
      		Console.WriteLine("Entity Framework Core Code-First sample")
      		Console.WriteLine()
      
      		MyDbContextSeeder.Seed(context)
      
      		Console.WriteLine("Products with categories")
      		Console.WriteLine()
      
      		Dim query = context.Products.Include(Function(p) p.Category).Where(Function(p) p.Price > 20.0).ToList()
      
      		Console.WriteLine("{0,-10} | {1,-50} | {2}", "ProductID", "ProductName", "CategoryName")
      		Console.WriteLine()
      		For Each product As var In query
      			Console.WriteLine("{0,-10} | {1,-50} | {2}", product.ProductID, product.ProductName, product.Category.CategoryName)
      		Next
      
      		Console.ReadKey()
      	End Sub
      End Class
      
      

    7. Now we can run the application. It will create tables in the database, fill them with data, execute a query and output its results to the console:
      Entity Framework Core Code-First sample
      
      Products with categories
      
      ProductID  | ProductName                                        | CategoryName
      
      1          | Harrison G. B. England in Shakespeare's day        | novel
      2          | Twain Mark. Ventures of Huckleberry Finn           | novel
      3          | Plutarchus. Plutarch's moralia                     | prose
      4          | Shakespeare W. Shakespeare's dramatische Werke     | prose
      5          | King Stephen. 'Salem's Lot                         | poetry

    See Also

    Entity Framework section  | Entity Framework Support Overview