2

I wanted to test multiple approaches in building an app (DB first, Model first, code first). After using T4Scaffolding and having lots of issues to DB post modifications, I though EF was not enough flexible. Now I have found a very weird thing. I left a single 'DefaultConnection' specified in Web.Config and is pointing to the single .mdf file in App_Data folder of the solution. Using a Code-First approach, I've created my entities (classes), then scaffolded repositories, context, everything seems to work almost fine, except I get even data which was stored before I 'deleted' and updated the DB. But, after checking in VS Server Explorer, the database only contains Tables used for Identity (Users, Roles), and this shows me that the actual database is somewhere else. I suspect it is located at 'C:\Users{MyUser}\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances'. But I cannot open .mdf files from there to check, since they are already in use. I am stuck. Where is my data???

Forgot to mention that I have two contexts in my application, therefore I receive a warning in PM Console: "More than one context type was found in the assembly ...".

Howerver, the first is 'ApplicationDbContext' and it only refers to Identity DB:

public class ApplicationUser : IdentityUser
{
}

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

and the second context is bound to that single DB Connection from Web.Config, and it has the business logic entities

Paul Pacurar
  • 145
  • 12
  • if you want to open an .mdf used by MS Server shut-down the SQL server service from services first. Moreover opening Management Studio and looking at the properties of the db you are instersted you can locate where the mdf is stored – apomene Jan 31 '14 at 15:02
  • but when I say 'open' I mean open in Server Explorer which uses an SQL Connection, which cannot be established with server stopped – Paul Pacurar Jan 31 '14 at 15:04
  • Server Explorer?...to which client program are you referring? – apomene Jan 31 '14 at 15:06
  • I use Server Explorer from Visual Studio 2013 – Paul Pacurar Jan 31 '14 at 15:10

1 Answers1

1

It is little weird. Connection strings are picked from Web.Config and it must be there. Please recheck. Also by default EF creates a database in the App_Data folder. So you can search that folder. Also In case if you find it uncomfortable working with multiple contexts, you can simply copy all the DbSets into ApplicationDbContext, and it should work fine.

Edit: You can specify the same CS for your other context like:

namespace MvcProject 
{     
    public class NorthwindDb : DbContext     
    {         
        public NorthwindDb() : 
            base("DefaultConnection") {}
    }
}  

Here while calling the constructor of base class, we can pass name of the connection string. By default Identity uses DefaultConnection and we can set this to our context.

Ashish Charan
  • 2,347
  • 4
  • 21
  • 33
  • I did check, but nothing new. The db file from App_Data is the same from Server Explorer, and same and the only one from Web.Config. There is no other connectionString property in the whole solution. But that db file only has the Identity tables. – Paul Pacurar Feb 03 '14 at 09:31
  • Try changing your other contexts connection string to point to DefaultConnection. – Ashish Charan Feb 03 '14 at 09:44
  • But where are the connection strings for context specified if not in Web.Config? – Paul Pacurar Feb 03 '14 at 09:51
  • I will try to do that too. But right now I'm developing my app and the app works as it is. There is no constructor defined in my context class. So there is a mystery how updating the database really works. The update scenario is: deleting migrations (if error occur), enable migration (specify my context), add new migration, update database. All these commands run in PM console and work. In the migrations configuration generated code file, Up() and Down() methods receive code which really updates and makes changes to the database. But where that database is... I really don't have a clue... – Paul Pacurar Feb 03 '14 at 10:10