Setting Up Entity Framework Core Database With Factory

In this write-up, I will explain how to easily and quickly set up your database using Entity Framework Core in a .NETCore project using the factory pattern to connect your entities to your database.

In this write-up, I will explain how to easily and quickly set up your database using Entity Framework Core in a .NETCore project using the factory pattern to connect your entities to your database.

Steps to reproduce:

Add a new project of type class library (.NET Core), named "EntityFrameworkFactory".

Make sure to have the respective NuGet packages inside this project.
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.EntityFrameworkCore.SqlServer (to connect to SQL Server)
  • Microsoft.EntityFrameworkCore

Add a new folder for models (Where you would put your entities).

Add a new class inside this folder and include some properties. This class will be our unique model used for this example. This class must have one property with the Key attribute. (One entity as an example with two properties).

  1. using System.ComponentModel.DataAnnotations;  
  2.   
  3. namespace EntityFrameworkFactory.Models  
  4. {  
  5.    public class ModelSample  
  6.     {  
  7.         [Key]  
  8.         public int IdModelSample { getset; }  
  9.         public string DescriptionModelSample { getset; }  
  10.     }  
  11. }  

Add a new class in the root of this project, named SampleContext. This class will be our context class that will be used to connect with the database. This class will inherit from DbContext.

Declare the sample model created as a dbset and set up the constructor.

  1. using Microsoft.EntityFrameworkCore;  
  2. using EntityFrameworkFactory.Models;  
  3.   
  4. namespace EntityFrameworkFactory
  5. {  
  6.   public  class SampleContext : DbContext  
  7.     {  
  8.         public SampleContext(DbContextOptions<SampleContext> options) : base(options)  
  9.         {  
  10.   
  11.         }  
  12.         public DbSet<ModelSample> SampleClass { getset; }  
  13.     }  
  14. }  
Create a new class in the project to be the dbContext factory, which will create your database, named SampleContextFactory. This class will inherit from the IDbContextFactory, with the type of the created context.
  1. using Microsoft.EntityFrameworkCore;  
  2. using Microsoft.EntityFrameworkCore.Infrastructure;  
  3.   
  4. namespace EntityFrameworkFactory
  5. {  
  6.     public class SampleContextFactory : IDbContextFactory<SampleContext>  
  7.     {  
  8.         public SampleContext Create(DbContextFactoryOptions options)  
  9.         {  
  10.             var optionsBuilder = new DbContextOptionsBuilder<SampleContext>();  
  11.             optionsBuilder.UseSqlServer("Data Source=YOUR DATA SOURCE;");  
  12.   
  13.             return new SampleContext(optionsBuilder.Options);  
  14.         }  
  15.     }  
  16. }  

At this point, you have to open the Package Manager Console (Tools > NuGet Package Manager > Package Manager Console) and set the default project to "EntityFrameworkFactory".

Execute this command (you may change the test to any name as you wish).
  1. add-migration test  
Execute the last command.
  1. Update-Database    
Congratulations! You have your Entity Framework Core database set up.