Question

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).

Was it helpful?

Solution 2

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.

OTHER TIPS

I had a similar problem. I scripted a backup of the database, added it to mssql server and pointed my connectionsstring to the new one instead.

Check the defaultConnectionFactory type in your config. I expect it is set to LocalConnectionFactory, as this seems to be default.

Change it to the following and your SQL instance will be used.

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True;" />
  </parameters>
</defaultConnectionFactory>

Your DB should appears in SQL Management Studio (SQLMS) with a name that matches the namespace.

Please do not be concerned. it is simply necessary to re-configure the connection string in the Web.config file accordingly: - Remove the "\SQLEXPRESS" postfix from the server name; - Remove the "User Instance=true" key/value pair.

If Your DbContext Class is StudentDbContext Then put Database=StudentDb;

Or

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top