简体   繁体   English

在n层体系结构中将ApplicationDbContext与依赖注入一起使用

[英]Using ApplicationDbContext with Dependency Injection in an n-layered architecture

I have an application with 3 layers (Presentation - Business - Data) built with Asp.Net MVC Core 2.1 我有一个使用Asp.Net MVC Core 2.1构建的具有3层(演示文稿-业务-数据)的应用程序

In my Presentation layer I have an ApplicationDbContext class which instantiates and fills a test database: 在我的Presentation层中,我有一个ApplicationDbContext类,该类实例化并填充测试数据库:

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        SeedData(builder);
    }

    // Database Tables
    public DbSet<Customer> Customers { get; set; }
    public DbSet<Ingredient> Ingredients { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderDetail> OrderDetails { get; set; }
    public DbSet<Pizza> Pizzas { get; set; }
    public DbSet<PizzaIngredient> PizzaIngredients { get; set; }

    // Fill Database with sample data
    private void SeedData(ModelBuilder builder)
    {
         // Seed data
    }

Said class is injected within the Startup.cs class (also in presentation layer): 所述类被注入到Startup.cs类中(也在表示层中):

        services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);

I now want to use this ApplicationDbContext class in the datalayer to keep code seperated. 我现在想在数据层中使用此ApplicationDbContext类来使代码分开。 How would I best go about this? 我最好怎么做? Injecting the class via constructor does not seem to work (Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'ApplicationDbContext' could not be found (are you missing a using directive or an assembly reference?)) 通过构造函数注入类似乎不起作用(严重级别代码描述项目文件行抑制状态错误CS0246找不到类型或名称空间名称'ApplicationDbContext'(您是否缺少using指令或程序集引用?)

namespace PizzaShop.Data.Repositories
{
   public class PizzaRepo : IPizzaRepo
   {
       private readonly ApplicationDbContext _context;

       public PizzaRepo(ApplicationDbContext context)
       {
          _context = context;
       }

       public async Task<int> AddEntityAsync(Pizza entity)
       {
           _context.Pizzas.Add(entity);
           return await _context.SaveChangesAsync();
       }
    //...
   }
}

Architecture: 建筑: 在此处输入图片说明

If you want to keep all database-related stuff in the PizzaShop.Data project, then your ApplicationDbContext doesn't belong in your web project. 如果要将所有与数据库相关的内容保留在PizzaShop.Data项目中,则ApplicationDbContext不属于您的Web项目。 It belongs in your PizzaShop.Data project. 它属于您的PizzaShop.Data项目。

You then reference your PizzaShop.Data project from the web project. 然后,您可以从Web项目中引用PizzaShop.Data项目。

Your ApplicationDbContext needs to be in the DataLayer. 您的ApplicationDbContext必须位于DataLayer中。

References come from bottom to top which means from Presentation Layer References Business Layer References Data Layer . 引用自下而上,这意味着来自Presentation Layer References Business Layer References Data Layer If you try to reference Presentation Layer in the Data Layer, cross reference problems occur. 如果您尝试在数据层中引用表示层,则会出现交叉引用问题。 (it doesn't even makes sense). (甚至没有意义)。

As a result, move your ApplicationDbContext to where it belongs, which is the Data Layer and everything will be sorted out :) 结果,将您的ApplicationDbContext移动到它所属的位置,即数据层,所有内容都将被整理:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET:具有存储库、服务和适当 DI 的 n 层架构 - .NET: n-layered architecture with repository, services and proper DI n层应用程序内的数据排序 - Data sorting inside n-layered application .net核心N层Web Api使用自动映射器在控制器层和服务层之间传递DTO错误 - .net core N-layered Web Api using automapper to pass DTOs between controller and service layers error 不使用ORM的N层数据库应用程序,UI如何指定数据显示的内容? - N-layered database application without using an ORM, how does the UI specify what it needs of data to display? Azure Key保管库连接字符串和N层设计 - Azure Key Vault Connection Strings and N-Layered Design EF:如何在N层应用程序中修改DB中的现有条目 - EF: how to modify existing entry in DB in N-layered application 如何管理内存中的主数据以避免损害n层应用程序中的数据库 - How to manage master data in memory to avoid harming database in n-layered application 在共享主机中部署N层ASP.NET Webforms应用程序 - Deploying an N-Layered ASP.NET Webforms Application In A Shared Hosting 3层架构中Unity的依赖注入 - Dependency injection with Unity in 3 tier architecture N层ASP.NET应用程序:我的所有图层的一个类库或每个图层的一个类库? - N-Layered ASP.NET Application: One class library for all my layers or one class library for each layer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM