Database error in ASP.NET MVC3 EF4.1 code first - asp.net-mvc

Hi i have a working app that uses an attached mdf file with the following connect string :
<add name="SqlDataContext" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=true;User Instance=true;AttachDBFilename=|DataDirectory|\WPCloudApp26.mdf;Initial Catalog=WPCloudApp26;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
I am using a Windows phone cloud application template and i have azure storage emulator running on my pc
everything works fine, until i do the following steps for the first time:
i want to view the database so i double click the app_data/WPCloudApp26.mdf file through visual studio when the app is not running. when i double click it, the db opens up under data connections in the server explorer pane and i can see the database and all the tables. so far everything good
I am done viewing the db, so i close the connection by right clicking --> close connection.
i want to run the app again, and when i run it all hell breaks loose. i get the following error message:
One or more files do not match the primary file of the database. If
you are attempting to attach a database, retry the operation with the
correct files. If this is an existing database, the file may be
corrupted and should be restored from a backup.\r\nCannot open
database \"WPCloudApp26\" requested by the login. The login
failed.\r\nLogin failed for user 'NT AUTHORITY\NETWORK
SERVICE'.\r\nLog file 'C:\Users\michael\Documents\Visual Studio
2010\Projects\WPCloudApp26\WPCloudApp26\WPCloudApp26.Web\App_Data\WPCloudApp26.ldf'
does not match the primary file. It may be from a different database
or the log may have been rebuilt previously.
the exception is thrown on the method var firstItem = context.SqlSampleData.FirstOrDefault();
Thats it, no matter what i do, i can't run the app again. this is driving me crazy since the only thing to help is to create a new solution with a different name (thats why i got to number 26)
What is going on ? why is this happening only after the first time i try to view the database in visual studio ?
Edit
i have noticed that changing the initial catalog and mdf file in the connection string solves the problem. So where are the remnants of the db name are saved ? how can i clean up old leftovers ?

You shouldn't try to open a .MDF file that is attached to SQL Server Express. It will probably break the consistency between the .MDF and the log file.
Connect to the SQL Server Express server using the Server Explorer, or SQL Server Management Studio. (There's a free version of it for Express as well)
If you really want to open the .MDF in Visual Studio, then detach the DB from SQL Server Express first.

Related

Where does ASP MVC - "Individual account authentication" project database gets stored?

I've encountered a strange thing...
I created a new project, with Individual account authentication authentication which creates a
DefaultConnection in Web.config.
I've also configured an oracle database connection, and commented the DefaultConnection.
Then I've registered a new user and data was saved in a hidden .mdf file that
was showed in App_Data after I've pressed "Show All Files" button in
Visual Studio.
I've deleted that .mdf file, Clean Solution, Build solution, Run project again -> login with same data and login still working
I'm very confused where did the app get's login data if no database exists with that info. Is it storing in another place that I don't know about?
It might be attached to local Db instance. Check Server Explorer to find if that is the case.If yes then you can delete the database from there.
I've found it eventually. Best way to make sure you see all databases on local device is to go to (in Visual Studio): View -> SQL Server Object Explorer -> Add SQL Server -> here you add all available servers (in my case was on my hostname\SQLEXPRESS)

MVC3 Problems After changing DB from SDF to MDF

Error:
An attempt to attach an auto-named database for file C:\Documents and Settings\xxx\My Documents\Visual Studio 2010\Projects\IncomeAndExpenditure\IncomeAndExpenditure\App_Data\Transactions.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
At the start of today I was happily accessing my MVC3 application's data in a SQL Server Compact Edition 4 .sdf data file. I now need to access this data from an Android app, but the Android app cannot connect to a .sdf file, it needs a connection to a .mdf file.
So I changed the database in the ASP.NET MVC app to a SQL Server Express and a .mdf file. So far so good, after much connection string changing the application runs under VS and connects to the new .mdf and allows read/write as expected.
Unfortunately now when I try to access the ASP.NET MVC app directly through a browser (I am running everything on localhost incl. VS, SSMS) I get the error as above. I have spent most of the day going back and forth as per many many posts here, MSDN and elsewhere to the point actually where the browser hung and I lost the will to live.
So after advice from the many posts this is I have tried so far in order to connect from both VS 10 and from MSIE 8 browser:
Placing database .mdf & log in SQL Server Express default data folder
Placing .mdf in |DataDirectory| (App_Data) folder
Adding new users in SSMS
Changing user permissions in SSMS
Altering User Instance=True / False in connection string
Attempting to connect after Attaching and detaching database .mdf & log files
My question:
Is there a straight forward guide somewhere that gives details of 'EVERYTHING' that needs to be done/considered when trying to swap a datasource from a SQL Server Compact Edition .sdf file to a SQL Server Express .mdf file?
I am using: XP, VS10, SQL Server 2008 R2, MSIE 8
Thanks, carl

ASP.NET MVC3 Aspnetdb Sql Production Server

I created a fully functioning web application (MVC MusicStore, downloaded from here: http://mvcmusicstore.codeplex.com/).
The application created an ASPNETDB.MDF file, and a MusicStoreDB.mdf file.
I used SQL Server Management 2008 R2.
The problem is that after I deploy the application on a host server,
when clicking on "Register" link, the default error page is displayed.
When trying to log in, the default error page is displayed yet again.
It appears that the host server (which is SQL 2008 compatible) can read data from the MusicStoreDB.mdf file, but has no access to the ASPNETDB.MDF file. It is not possible to log in or register users.
I did change the connection string in the web.config file to the host server address, it works fine.
I also created the ASPNETDB tables in the MusicStoreDB by using aspnet_regsql, but the errors persist.
Also, the MusicStoreDB file contained no data after Publish (though there is sample data in a cs file). I know the host server can read data from this file because I manually added data through a local SQL server.
I have two questions:
a. What are the steps to correctly deploy the application and databases so that it would be possible to register and log in? How can the both mdf files work in consort?
b. How can the data in the SampleData.cs file be recreated in the MusicStoreDB.mdf file?
I haven't been able to find an answer over the web, though I've been searching for a week...

MVC 3 Adding a local Database

I am currently going through the MvcMusicSore tutorial (MVC 3). I have full sql server 2008 installed and created a local database by running the SqL scripts included in the MvcMusicStore-Assets data folder. However when trying to add the mdf to the AppFolder in Visual Studio 2010 I get the error Access Denied. I am completely stuck at this point and would appreciate any help to resolve this.
Most probably the mdf file is locked by some other process, not allowing the application to read it. If you mounted the database on SQL Server you need to use a connection string with the machine name instead of specifying the mdf file directly.
You can't copy or modify a live working database. And I don't see why you should.
You need connecting to it? Pick a way. LINQ to SQL, Entity Framework, NHibernate, ADO.NET...
If you really want to copy the database file for some reason, you must first stop the MSSQL Service (or detach the database), then do that.
Like others have said, you shouldn't need to add the actual .mdf into your project. If you have it running on your local SQL Server instance, you should be able to add it via Visual Studio's Server Explorer (plus that gets you your connection string).

When attaching a SQL Express DB via a connection string, is it an exclusive lock?

In .NET, you can open a SQL Express database simply by attaching the database MDF file in the connection string (that is, you don't have to have any server software installed, it just attaches to the MDF file on the fly).
When an application does this, does it obtain an exclusive lock on the database file? Or, can a second application also attach to and open the MDF file in this same way, and query the database while program #1 is using it?
You are correct, only a single connection can be open to the MDF at a time.
This can get really annoying when you open the database through your server explorer in Visual Studio, and then try to run your application.
I've caught myself doing that too many times to count, and I always invariably end up just attaching the mdf to my sql server instance.

Resources