Alex K
Alex K

Reputation: 51

EF Core in .NET Core library

The main idea is to add a EF Core nuget package to a .NET Core library project and then use that library in a bunch of applications (e.g., ASP.NET Core, Win Service, Console App) without configuring EF in each of them. And, if possible, without adding EF packages to each of them. I'm wondering if it's possible.

My current problem is that I can't create a database based on the model I have in the library project.

It seems I can't just select the library project in the Package Manager Console and run update-database. It wants me to implement 'IDesignTimeDbContextFactory'.

I'm using .NET Core 2.1. Would it help if I update it to the latest version?

Upvotes: 4

Views: 1928

Answers (2)

Brian Olson
Brian Olson

Reputation: 271

Yes, you can do this.

Make sure you have all the prerequisites installed.

  1. Create a .NET Core Console app
  2. Create a Core Class library for Entity Framework
  3. Reference the Class library from the Console App
  4. Scaffold your database, go to Tools > Package Manager Console
  5. From the dropdown set your default project to your class library so it will scaffold there.
  6. Run this in the console (database first approach): Scaffold-DbContext "Your connecting string here" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models/EF -f
  7. Create a class to get your context
public class Context
    {
// See all queries generated by EF in debug window
        public static readonly LoggerFactory MyLoggerFactory
            = new LoggerFactory(new[] { new DebugLoggerProvider((s, level) => level >= LogLevel.Trace) });
        public static DF.Data.Models.EF.YourContext GetContext()
        {
            var optionsBuilder = new DbContextOptionsBuilder<DF.Data.Models.EF.YourContext>();
            optionsBuilder.UseSqlServer(
                "Your Connection String").UseLoggerFactory(MyLoggerFactory);
            return new DF.Data.Models.EF.YourContext(optionsBuilder.Options);
        }

        public partial class YourContext : DbContext
        {
            public YourContext(DbContextOptions optionsBuilderOptions) : base(optionsBuilderOptions)
            {
            }
        }
    }
  1. Create a Repository class to store your queries if you would like.

Note: When you scaffold the database again make sure you select the Class library project as the default project from the dropdown. Then set your other project back to the startup project.

Upvotes: 2

Jan Paolo Go
Jan Paolo Go

Reputation: 6542

As mentioned by the error, you need to implement IDesignTimeDbContextFactory which is part of the Microsoft.EntityFrameworkCore.Design package so go ahead and install that in your library. Then create a class that implements IDesignTimeDbContextFactory appropriately.

Since you created a .NET Core library, set that as your startup project.

Then in your Package Manager Console, select your library as the Default project and run update-database.

Upvotes: 2

Related Questions