将连接字符串发送到ApplicationDBContext

Thr*_*gic 7 c# owin asp.net-mvc-5 asp.net-identity

我已经定制了ApplicationDBContext类来通过其构造函数接收连接字符串.它可以在直接调用时连接到数据库OK但是当通过app.CreatePerOwnContext调用时我无法调用它.我的课程定义如下:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(string databaseConnection)
        : base(databaseConnection, throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create(string databaseConnection)
    {
        return new ApplicationDbContext(databaseConnection);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是由以下行中的Startup.Auth.cs调用它.

app.CreatePerOwinContext(ApplicationDbContext.Create);
Run Code Online (Sandbox Code Playgroud)

create方法也采用连接字符串,但以下方法不起作用

app.CreatePerOwinContext(ApplicationDbContext.Create(connectionString));
Run Code Online (Sandbox Code Playgroud)

它会产生以下错误:

Error   1   The type arguments for method 'Owin.AppBuilderExtensions.CreatePerOwinContext<T>(Owin.IAppBuilder, System.Func<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Run Code Online (Sandbox Code Playgroud)

将连接字符串发送到ApplicationDbContext类的正确语法是什么,以便Owin上下文可以引用它?

连接字符串是正确的,但为了完整性,设置它的代码如下.

string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 13

请查看您使用的方法的声明:

public static IAppBuilder CreatePerOwinContext<T>(
    this IAppBuilder app,
    Func<T> createCallback) where T : class, IDisposable
Run Code Online (Sandbox Code Playgroud)

期待这种类型的论证Func<T>.

所以你需要将代码更改为:

app.CreatePerOwinContext(() => ApplicationDbContext.Create(connectionString));
Run Code Online (Sandbox Code Playgroud)