EF Core 5 - Creating Libraries That Uses EFCore Including Migrations

Viewed 122

I'm creating a library for managing permissions. The library exposes multiple methods to create, attach, bind, update, delete permissions and permission groups.

This is the GitHub project link

It uses SQLServer and some pre-defined tables to store those. It uses EFCore 5

Now my question is, If I shipped my library that depends on EFCore. How my end user address these?

  1. If he installs it from NuGet - How he could run the migrations inside it? so that required tables will be created on his DB. Can he run Update-Database on his PM targeting the library?

        PM:\ Update-Database
    
  2. If it's a NuGet library how can I allow him to pass a connection string from his project where he is installing it. I know about passing like this to point to a library project, Is that the same need to follow? Does this require him to install any separate EFCore related libs?

         services.AddDbContext<PermissionContext>(options =>
         {
             options.UseSqlServer(Configuration.GetConnectionString("JetTask"),
             assembly => assembly.MigrationsAssembly(typeof(PermissionContext).Assembly.FullName));
         });
    
  3. If he's already using another ORM like Dapper, Will he still need to install any other dependencies to run my library since it depends on EFCore? Or does the main library installs the dependency components from NuGet while he installs?

1 Answers

I have never attempted to provide a solution via NuGet myself, but taking my lead from using .Net Identity Framework ...

If he installs it from NuGet - How he could run the migrations inside it? so that required tables will be created on his DB. Can he run Update-Database on his PM targeting the library?

Migrations are based on the difference between the model and the target database. So, the consumer of your NuGet package would target your DbContext (configured to use their own database) and generate their own migrations that will be the difference between your model encapsulated in your DbContext and their database tables. I don't think the concept of "migrations inside it" should be relevant.

If it's a NuGet library how can I allow him to pass a connection string from his project where he is installing it. I know about passing like this to point to a library project, Is that the same need to follow? Does this require him to install any separate EFCore related libs?

I think all of the libs that your project have a dependency on will be automatically installed with the NuGet package. Yes, they should be able to configure your context:

 services.AddDbContext<PermissionContext>(options =>
 {
     options.UseSqlServer(Configuration.GetConnectionString("JetTask"),
 });

If he's already using another ORM like Dapper, Will he still need to install any other dependencies to run my library since it depends on EFCore? Or does the main library installs the dependency components from NuGet while he installs?

Pretty sure, as mentioned above, the dependencies will be installed with the NuGet package.

If I were in your position, I'd create the NuGet package and then create a new project, install the NuGet package and 'live the life of the consumer' to identify any gotchas.

Related