简体   繁体   English

首先为ASP.NET应用程序数据库实现OWIN数据库上下文

[英]Implementing OWIN Database Context for ASP.NET Application Database First

The book I'm reading says that I should use OWIN to get an instance of my database context for using Identity... but I'm using database first so I want to have an instance of my database context for use throughout my application. 我正在阅读的这本书说我应该使用OWIN获取我的数据库上下文的实例以使用Identity ...但是我首先使用数据库所以我希望在我的应用程序中使用我的数据库上下文的实例。

I've implemented the 'Start' class for OWIN in the following way: 我已经通过以下方式为OWIN实现了'Start'类:

using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using uQuiz.Domain;
using uQuiz.Domain.Concrete;
using uQuiz.WebUI.Infrastructure;

namespace uQuiz
{
    public class OwinStart
    {
        /// <summary>
        /// Configures OWIN
        /// </summary>
        /// <param name="app"></param>
        public void Configuration(IAppBuilder app)
        {
            app.CreatePerOwinContext<QuizEntities>(GetQuizEntities);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        }

        /// <summary>
        /// Returns an instance of the QuizEntities database context for OWIN
        /// </summary>
        /// <returns></returns>
        public static QuizEntities GetQuizEntities()
        {
            return new QuizEntities();
        }
    }
}

As you can see, I have a function there that just produces a single instance of my database context QuizEntities , which is database first so automatically generated. 正如您所看到的,我在那里有一个函数,只生成我的数据库上下文QuizEntities的单个实例,它首先是数据库,因此自动生成。

I know that it is normal practice to have a 'Create' method within your database context, but I can't do this because it is automatically generated. 我知道在数据库上下文中使用“Create”方法是正常的做法,但我不能这样做,因为它是自动生成的。

My Questions: 我的问题:

  • Is there a down side of creating an instance 'per OWIN' in this way? 以这种方式创建一个“每个OWIN”实例是否有缺点?
  • Is this an acceptable way of implementing it? 这是一种可以接受的实施方式吗?
  • Is it acceptable to use the same database context for Identity and other requests within the application? 在应用程序中对Identity和其他请求使用相同的数据库上下文是否可以接受?

You are creating a database context per OWIN context, not per OWIN. 您正在为每个OWIN上下文创建数据库上下文,而不是每个OWIN。 It's not a single instance. 这不是一个单一的实例。 For each incoming request, a database context is created. 对于每个传入请求,都会创建一个数据库上下文。 So OWIN calls GetQuizEntities for each request to create a new database context. 因此,OWIN为每个创建新数据库上下文的请求调用GetQuizEntities

Is there a down side of creating an instance 'per OWIN' in this way? 以这种方式创建一个“每个OWIN”实例是否有缺点?

No, you must do it the way you are doing now, otherwise your db context will get bloated. 不,您必须按照现在的方式执行此操作,否则您的数据库上下文将变得臃肿。 Create a new db context per request. 每个请求创建一个新的db上下文。 Right. 对。

Is this an acceptable way of implementing it? 这是一种可以接受的实施方式吗?

Yes it is. 是的。

Is it acceptable to use the same database context for Identity and other requests within the application? 在应用程序中对Identity和其他请求使用相同的数据库上下文是否可以接受?

I'm not sure what you mean there. 我不确定你的意思。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM