Send connection string to ApplicationDBContext

I configured the ApplicationDBContext class to get the connection string through my constructor. It can connect to the OK database when called directly, but when called through app.CreatePerOwnContext, I cannot call it. The definition of my class is below:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(string databaseConnection) : base(databaseConnection, throwIfV1Schema: false) { } public static ApplicationDbContext Create(string databaseConnection) { return new ApplicationDbContext(databaseConnection); } } 

The problem is that it is called Startup.Auth.cs in the next line.

 app.CreatePerOwinContext(ApplicationDbContext.Create); 

The create method also accepts a connection string, but the following does not work

 app.CreatePerOwinContext(ApplicationDbContext.Create(connectionString)); 

The following error is issued:

 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. 

What is the correct syntax for sending a connection string to the ApplicationDbContext class so that the Owin context can reference it?

The connection string is correct, but for completeness, the code that installs it is below.

 string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; 
+5
source share
1 answer

Please check out the declaration of the method you are using:

 public static IAppBuilder CreatePerOwinContext<T>( this IAppBuilder app, Func<T> createCallback) where T : class, IDisposable 

An argument of type Func<T> is expected.

So you need to change your code to:

 app.CreatePerOwinContext(() => ApplicationDbContext.Create(connectionString)); 
+12
source

All Articles