0

I am currently working on a project where I am developing a class library that later on will be uploaded as a nugget package, such that if a user creates a.NET Core application, she/he can download the nugget package and use it accordingly.

Essentially within the class library, Entity Framework, Nethereum and other packages are installed as dependencies. One of my goals is not to require users to add Entity Framework to their application (since the nugget package (, i.e. the class library I am building)) already has it installed. For that reason, there is a DbContext that accepts the database connection string in the class library and builds the options.

public class BEFDbContext: DbContext
{
    
    public BEFDbContext(string connectionString) : 
        base(SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options) { }

    public DbSet<ApplicationEvent> Events { get; set; }

}

Next, the user has to create another class in the application code that extends the BEFDbContext class found in the class library.

public class NewDatabaseContext: BEFDbContext
{        
    public NewDatabaseContext(string connectionString):base(connectionString){}
}

So far so good, however, at this point, I would like to 'initialise' the NewDatabaseContext class in the Startup.cs class. Generally, one would use Entity Framework and would add the code as such:

services.AddDbContextPool<NewDatabaseContext>(options =>
        {
            options.UseSqlServer(Configuration.GetConnectionString("defaultconnection"));
        });

However, as I mentioned before One of the goals is to not require users/developers to add Entity Framework to the application (once again since we have it in the class library).

So, my question is How I can add the NewDatabaseContext class as DbCcontext in the Startup.cs without using Entity Framework?

5
  • Entity is using dbContext which is the base class of all Data connections. So BulkCopy will work. See : stackoverflow.com/questions/2553545/… Commented Apr 28, 2021 at 17:41
  • Hey. Just to let you know... Asp Net Core has dependency inheritance, ie: if a nuget package references EntityFramework then your application automatically has this Commented Apr 28, 2021 at 17:43
  • @IhusaanAhmed first of ty for reply, what you are saying is that I could use the "General" approach to add the DbContext class? meaning I could directly use the BEFDbContext (which is in the class library) instead of NewDatabaseContext (in the application)? Commented Apr 28, 2021 at 17:48
  • sorry for the late reply but what i am saying is that even though you haven't directly added the EntityFrameworkCore Nuget to the application, you can use AddDbContext or AddDbContextPool since that nuget package will be "inherited" by the application from the class library. if you want another way you can use extension methods too. if you want i can show you how to do that in an answer Commented Apr 29, 2021 at 4:04
  • Just by simply 'pointing me out' that I can do this through inheritance, yet in another way, I could overcome this problem. Thank you for your advice. Although this is an alternative solution, the specific question made here was not answered yet. Commented Apr 29, 2021 at 10:56

1 Answer 1

0

Since you wanted the alternative response you can use Extension methods

in your library add the following code

public static class ServiceCollectionExtensions
{
    public IServiceCollection AddApplicationDbContext<T>(this IServiceCollection services, IConfiguration configuration) where T : BEFDbContext
    {
        services.AddDbContextPool<T>(options =>
        {
            options.UseSqlServer(Configuration.GetConnectionString("defaultconnection"));
        });
        return services;
    }
}

then in the startup of application you can use

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddApplicationDbContext<NewDatabaseContext>(Configuration);
    ...
}

You can have variations of this as per your need. Like accepting the connection string instead of the whole Configuration, etc.

This answer uses generics and extension methods. If you want more details then please checkout:

Generic methods: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-methods

Extension Methods: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your answer, Ihusaan Ahmed. This approach makes a lot of sense, and it works as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.