简体   繁体   English

如何在没有 HTML 页面的情况下获得 dbcontext 访问权限?

[英]How to get dbcontext access without an HTML page?

How can I get access to the dbcontext without an HTML page?如果没有 HTML 页面,如何访问 dbcontext? I have a WebAPI project in which the classes from the HTML page can access the database, like this:我有一个 WebAPI 项目,其中来自 HTML 页面的类可以访问数据库,如下所示:

    public class CreateModel : PageModel
    {
       private readonly Home5.Models.Home5Context _context;

       public CreateModel(Home5.Models.Home5Context context)
       {
           _context = context;
       }

       public IActionResult OnGet()
       {
           return Page();
       }
    }

But when I create classes of my own, I can't access the context, because I can't create an instance of the class without having the context to give it to the constructor.但是当我创建自己的类时,我无法访问上下文,因为如果没有将上下文提供给构造函数,我就无法创建 class 的实例。

This sample shows the steps that are necessary to create a Web API that accesses a database using entity framework.示例显示了创建使用实体框架访问数据库的 Web API 所需的步骤。

In order to use your Home5Context , the crucial steps are the following:为了使用您的Home5Context ,关键步骤如下:

File startup.cs文件启动.cs

Register the context with the dependency injection container.向依赖注入容器注册上下文。 This is the component that creates the controller instances and provides them with an instance of your context, if configured correctly:这是创建 controller 实例并为它们提供上下文实例的组件(如果配置正确):

services.AddDbContext<Home5Context>(opt =>
           opt.UseInMemoryDatabase("Home5Database"));

Please note that there are several other options besides an InMemoryDatabase.请注意,除了 InMemoryDatabase 之外,还有其他几个选项。 The following documentation shows most of them.以下文档显示了其中的大部分。

Controller Controller

Inject the context into the controller:将上下文注入 controller:

public class MyController : ControllerBase
{
    private readonly Home5Context _context;

    public TodoItemsController(Home5Context context)
    {
        _context = context;
    }

    // ...

After that, you can use the context and access the database in the controller.之后,您可以使用上下文并访问 controller 中的数据库。


Please see the sample or the code .请参阅示例或代码 Maybe it helps to follow the sample in a new project and then change your current project.也许它有助于在新项目中遵循示例,然后更改您当前的项目。

When I have a random class, how can i get an instance of context.当我有一个随机的 class 时,我如何获得上下文实例。

Sample:样本:

public class work()
{
    public void setValue()
    {
       var context = await _context.firstordefaultasync(....);
       context......
       //Work with that context
       ...save context...
    }
}

In this form I cant create an instance of the class for working with it:在这种形式中,我无法创建 class 的实例来使用它:

public class MyController : ControllerBase
{
    private readonly Home5Context _context;

    public TodoItemsController(Home5Context context)
    {
        _context = context;
    }
// ...

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

相关问题 从Asp.Net Core _Layout视图访问数据库 - Access database from Asp.Net Core _Layout view 我可以在ASP.NET Core启动期间访问数据库吗? - Can I access a database during startup in ASP.NET Core? 如何将 .NET Core ASP 应用程序连接到 MS Access 数据库 - How to Connect .NET Core ASP Application to MS Access database 通过SQL Server依赖注入刷新ASP.NET Core中的Azure Active Directory访问令牌 - Refreshing Azure Active Directory access token in ASP.NET Core with dependency injection for SQL Database 从 ASP.NET Core MVC 中的 SQL 数据库访问用户 ICollection - Access User ICollection from SQL database in ASP.NET Core MVC 如何在ASP.Net Core2 cshtml文件中访问数据库(查看) - How to access database in ASP.Net Core2 cshtml file (View) 如何在没有数据库的情况下在 ASP.NET Core 中创建一个简单的登录并授权控制器访问 - How to create a simple login in ASP.NET core without database and authorize controller access ASP.NET-Core应用程序是否在需要访问声明的任何时间访问数据库? - Does the ASP.NET-Core application hit the database anytime it requires access to claims? ASP.NET Core 2.1,C#应用程序,C#Web服务,访问SQL数据库 - asp.net core 2.1, C# app, c# web service, access SQL database ASP.Net Core帖子访问被拒绝 - ASP.Net Core Post Access Denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM