asp.net-mvc-3entity-frameworksql-server-2008-r2ssmsserver-explorer

ASP.NET MVC 3, SQL Server: Tables not appearing in explorer, but they're there


My database is stored in an .mdf in App_Data and is functioning fine. My application can add, edit, delete records.

But the tables are invisible. When I open the Server Explorer and attach my .mdf and try to view the tables, there are none listed there. I attached the file to SQL Server Management Studio as well, but the only tables that show up there are those in the folder System Tables.

From what I've been able to glean from the technobabble on MSDN this could be a permissions or ownership issue. I don't know about permissions, given that I'm able to connect to the database and query it and edit/delete records. So maybe ownership; I read somewhere that tables not owned by dbo may not show. But if that's the case, I don't know what my application's ownership name is or how to make Management Studio or even Server Explorer show tables owned by other users.

Here's the connection string, in case the answer's in there:

<add name="EFDBContext" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=|DataDirectory|MLDatabase.mdf;Database=EFDbContext;User Instance=true" providerName="System.Data.SqlClient"/>

Thanks to anyone who might point me in the right direction!

ETA. this seems like a similar issue, but I don't know if the solution there would apply, since my database wasn't generated by scripts but by the Entity Framework (Code-First model).


Solution

  • The reason the tables weren't showing up is because they were not in the .mdf, of course. As I noted in a comment, I found them elsewhere, in a location chosen by SQL Server.

    A workaround is to just let EF build the SQL Server database where it wants, stop the SQL Server service, move it to the App_Data folder, change the connection string appropriately, and then attach it to the ASP.NET project.

    Another issue is that SQL Server's Network Service does not have permission to do anything in the C:\Users\Me folder, or the Documents and Settings folder, until you go to that folder and assign permission to the Network Service directly. Then and only then will you be able, for instance, to attach the database to the SQL Server Management Studio. This is true even when Visual Studio installs SQL Server... and then denies it permission to access the files where VS keeps it's own projects. I don't get it; IMO any developer who's aware of this, the first thing she is going to do, just in order to get her work done, is to give Network Service that permission anyway.