3

I was working at an asp.net MVC project with the IdentityDbContext. The code for the context:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
   public ApplicationDbContext()
    : base("ApplicationDb")
   {
   }
 }

And in my web.config a connectionstring named after the context:

<add name="ApplicationDb" connectionString="Data Source="..." providerName="System.Data.SqlClient" />

Strange thing is when I call the Update-Database command to create the database with Entity Framework, my database is created. So far so good.

But: the authorisation code from Owin is also creating a second upon running the application. This one is named DefaultConnection and is copy from the other one.

So my question: does Identity Framework always need a connection string named "DefaultConnection", even if you point the context to another connectionstring?

In the end I managed to solve this by adding the DefaultConnection connectionstring in web.config so I end up with two connectionstring:

  1. ApplicationDb
  2. DefaultConnection

Is this really the way to go? Because if that's the case it doesn't make much sense to put a custom connectionstring name in the base constructor?!

Btw, I also tried the context like so:

 public ApplicationDbContext()
 {
 }

Which in theory should effectively do the same. But still DefaultConnection is created upon running the app. Doesn't make sense to me.

  • possible duplicate of [How to add ASP.NET MVC5 Identity Authentication to existing database](http://stackoverflow.com/a/25651908/296861) – Win Feb 20 '15 at 20:15
  • Not entirely true. My point: If you put a context and **don't** add a constructor to point to a connectionstring name (like in the last code segment of my original post), in theory it should be using the connectionstring which name corresponds to the classname of the context. In my case though it doesn't and instead creates a database called DefaultConnection and uses that. This doesn't seem right to me! – Willem van Beem Feb 20 '15 at 20:34
  • Could you search `DefaultConnection` inside your application? Do you find any? – Win Feb 20 '15 at 20:39
  • Nope, already did a find in the solution and only found the keyword in some commented code. Could it be that IdentityDbContext relies on this connectionstring to be there if you don't supply a connectionstring name in the context?? Doesn't make sense to me since the whole point is that it searches for a connectionstring with a name corresponding to the context class name. I'm puzzled here and it's working now with the extra DefaultConnection in web.config, but I really got the idea this is not needed. – Willem van Beem Feb 20 '15 at 20:49
  • I agree with you. It should not create **DefaultConnection** connection string if you pass **ApplicationDb** in constructor. Could you able to replicate the issue in new project? – Win Feb 20 '15 at 21:49
  • By default it goes with LocalDb. I tried with this code: public ApplicationDbContext() : base("TestConnection", throwIfV1Schema: false) { } And this connection – Willem van Beem Feb 20 '15 at 23:28
  • @WillemvanBeem Could you please tell me if you found a way not to specify a ConnectionString? I don´t need the Identity to create the DefaultConnection database because I´m implementing my own authetication inside Identity. Thank you in advanced! – Ignacio May 19 '20 at 22:39

0 Answers0