ASP NET CORE 5 EF with DLL database-connected

Asked

Viewed 35 times

-1

It is possible to use a database connection DLL in an ASP NET CORE 5 project with Entity Framework?

I have a DLL named "Bancodados" as I can use the methods of this DLL in the context of the project.

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ChamadosContext>(options => options.UseSqlServer("**NOME DA DLL**")));
    
    services.AddDatabaseDeveloperPageExceptionFilter();
    services.AddControllersWithViews();
    services.Configure<CookieTempDataProviderOptions>(options => {
        options.Cookie.IsEssential = true;
    });
}
  • You failed to reference the library within your project and instantiate its objects?

  • Not that way... This dll is probably called BancoDados by implementing a repository’s infra layer. You can connect to the bank and use the public classes and methods that are available in it...

  • Thanks for your attention, I can use the class objects, my question is how do I do this, indicating to my context that the connection is through the DLL. I don’t know if it was clear, basically I need the EF to use the DLL when making migrations and updates in the database.

  • It won’t, unless the connection variables are in constants in the DLL, which is unlikely and none a little recommended... Migrations will have their record in the database itself and not from the DLL, which you are intending doesn’t make any sense, why you think you need to do this and this way?

  • Ps.: If entities or models are in this dll... you will not be able to change without an override, so it has no impact on migrations unless you create new ones

1 answer

-1

It is possible to use a database connection DLL in an ASP NET CORE 5 project with Entity Framework?

Yes, and you certainly have. The Entity Framework, for example, is "a DLL connection to the database". According to good practices, your project should also have a repository layer, which in turn will also be "a DLL connection to the database".

However, the concept and use do not seem to be clear to you. The first step in trying to use a method should be to always look at your documentation, allowing you not only to understand whether it is suitable for your need or whether there is a better alternative in the same class.

The method UseSqlServer that you are using can be found at this link. By the documentation of the methods it is possible to notice that there are 2 overloads: one receives a string (more specifically a ConnectionString) and the other one DbConnection (a database connection object).

Therefore, the first correction should be to the line services.AddDbContext<ChamadosContext>(options => options.UseSqlServer("**NOME DA DLL**")));, can be replaced by something like services.AddDbContext<SchoolContext>(options => options.UseSqlServer(Configuration.GetConnectionString("NomeDaConexaoNoAppSettings")));.

Another alternative would be to use a method (which may be in that your DLL) that returns a connection string or a DbConnection, but in both cases the connection data would need to be fixed in the DLL, which, besides being nothing to recommend, also doesn’t make much sense.

What you seem to want is that yours models and DbContext are in a DLL (or separate project). And this will work normally when specifying AddDbContext<ChamadosContext>. Both the application and the migrations will use the DbContext specified in the application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.