A day with .Net

My day to day experince in .net

Archive for the ‘EfCore’ Category

EF Core DbContext in separate library

Posted by vivekcek on May 26, 2018

I was designing an ASP.NET core application with a layered architecture, and decided to keep my DbContext in a separate class library project.
This is normal in layered architecture right?. But when i tried to create migration got some strange error.

After some googling i found that we need to add below code in our library , when our DbContext and models are in separate class library.

Add below code in the library where your DbContext is, Here MyDbContext is the name of DbContext.

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace DataLayer
    public class MyContextFactory : IDesignTimeDbContextFactory<MyDbContext>
        public MyDbContext CreateDbContext(string[] args)
            var builder = new DbContextOptionsBuilder<MyDbContext>();
            return new MyDbContext(builder.Options);

Posted in EfCore | Leave a Comment »