问题描述:

I have created a project structure like below (using ASP.NET Core)

  • MyProject

    • MyProject.Data
    • MyProject.Business
    • MyProject.Web
    • ...

In this, Business project references the Data project and Web project references the Business project. Then I have added the Entity Framework Core library to Data project and created a Database Context class.

public class MyDbContext : DbContext

{

public SlcDbContext(DbContextOptions<MyDbContext> option)

: base(option)

{}

public DbSet<Test> Tests { get; set; }

}

Now I'm trying to register MyDbContext with ASP.NET Core built in Dependency Injection Service, for that I need to add below code to Startup.cs in the Web project

public void ConfigureServices(IServiceCollection services)

{

services.AddDbContext<MyDbContext>(options =>

options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

...

}

but this code requires reference to EntityFramework Core library and Data project.

Is there any way to deal with this without adding the unnecessary references to the Web project (as UI layer should be independent from the Data layer)?

相关阅读:
Top