0

I have a c# class libary that has its own database context and Entity Framework migrations.

My solution contains a web site that references the class library as a Project Reference.

I can apply the EF migrations for the Class Library in Visual Studio from Package Manager Console by setting the default project to my Class Library and using the following command:

update-database -StartupProject MyMainWebProj -Context MyClassLibDbContext

I have created a private NuGet package for the class libary and updated the Web Site to reference the NuGet package rather than a project reference.

I cannot figure out how to run the EF migrations now that the class library is referenced via NuGet package.

1 Answer 1

2

You may use the builder object of the Use*() method during service configuration like mentioned here: https://github.com/jasontaylordev/CleanArchitecture/blob/d0f133ee026aec5cd5856c5592c307b5f20fa8e4/src/Infrastructure/DependencyInjection.cs#L28

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(
        configuration.GetConnectionString("DefaultConnection"),
        b => b.MigrationsAssembly(typeof(SomeClassInTheOtherAssembly).Assembly.FullName)));
1
  • Thank you so much! I added the above to the Startup class of my Web site, (even though the context is not used in the web site - so the context is in effect just a dummy context in order to run the migrations). Now I can run migrations from the NuGet library using the standard "update-database" command from Package Manager console. Apr 14, 2021 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.