在ASP.NET核心中使用时,如何从另一个项目访问EF核心的DbContext? [英] How to I access the DbContext of EF core from another project when used in ASP.NET core?

查看:152
本文介绍了在ASP.NET核心中使用时,如何从另一个项目访问EF核心的DbContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了将EF Core与ASP.NET core结合使用的模式,一切都很好。但是最近我创建了一个计算项目,并希望从中进行数据库调用。

I followed the pattern to use EF Core with ASP.NET core and all is well. But recently I created a 'Calculation' project and want to make database calls from it.

问题是我不知道如何创建新的 DbContextOptions 。在我的代码中使用

The problem is I don't know how to create a new DbContextOptions. In my code that is done with

   services.AddDbContext<RetContext>(options => options
            .UseLazyLoadingProxies()
            .UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

但是在新的.NET核心类中,我需要手动提供它。我该怎么做呢 ?我的代码是这样的:

But in a new .NET core class I need to provide it manually. How do I do this ? My code is like this:

 public static class LoadData
{
    public static IConfiguration Configuration { get; }

    public static RefProgramProfileData Load_RefProgramProfileData(string code)
    {
        // var optionsBuilder = new DbContextOptionsBuilder<RetContext>();
        // optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));

        //How do I make an optionsbuilder and get the configuration from the WEB project?
       UnitOfWork uow = new UnitOfWork(new RetContext(optionsBuilder));


        var loadedRefProgramProfileData  = uow.RefProgramProfileDataRepository
            .Find(x => x.ProgramCode == code).FirstOrDefault();

        return loadedRefProgramProfileData;
    }
}


推荐答案

您可以像这样实例化您的 DbContext

You may instantiate your DbContext like this:

var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
var configuration = builder.Build();
var optionsBuilder = new DbContextOptionsBuilder<RetContext>();
optionsBuilder.UseSqlServer(configuration.GetConnection("DefaultConnection"));
_context = new RetContext(optionsBuilder.Options); 






但是,理想的情况是使用依赖注入。假设您在其他项目中有一个 CalculationService 类。为此,您需要将该类注册为可以注入的服务:


However, the ideal is to use dependency injection. Let's say you have a class CalculationService in your other project. For that, you need to register that class as a service that can be injected:

services.AddScoped<CalculationService>();

然后您的班级将收到 DbContext (或任何其他服务)通过DI:

Then your class can receive DbContext (or any other services) through DI:

public class CalculationService
{
    private RetContext _context;

    public CalculationService(RetContext context)
    {
        _context = context;
    }
}

自然,您将无法实例化您的实例像这样的手动类:

Naturally, you won't be able to instantiate your class manually like this:

var service = new CalculationService();

相反,您需要使任何类都需要使用 CalculationService 也可以通过DI接收它,并使该类也可以注入。

Instead, you'd need to make whatever class needs to use your CalculationService to also receive it through DI and make that class injectable as well.

这篇关于在ASP.NET核心中使用时,如何从另一个项目访问EF核心的DbContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆