Attached SQL Express DB is causing problems - asp.net-mvc-3

I have been asked to create an MVC web application in VS 2010, and was instructed to use a SQL express database for my data. I am using EF Code-First for creating and managing my data. The database was created in VS2010, and is attached via "AttachDBFilename" in the web.config.
I have used SQL CE before with MVC with no problems, however the attached SQL Express DB is causing weird issues.
For one thing, when I try to deploy the app, it fails and tells me that it cannot copy the database.mdf because it is in use by another process. I have NOT opened the database in VS2010 nor SSMS. Of course the program code accesses it - is there some reason that connection would remain open? I am using boilerplate code from the scaffolding.
I should mention that I use a ProjectInitializer.cs to create the sample data. It runs at every launch for the moment, since I am testing quite a bit.
The other problem I have is that if I delete the database, it fails to recreate it. It says that my windows account does not have access to the (now non-existent) database that it is trying to create. I literally have to create a new database with new name, as anything that was created previously (with that DB name) fails.
I assume there is some sort of residual info being left somewhere that is out of synch, but I don't know what it is. I've closed all connections to the file in VS 2010, deleted the files, both any found via VS2010 and any physical files I see in the app_data directory.
Any help or suggestions would be appreciated.

Shut down the web server (Cassini, IIS, IIS Express) and try again. The file can remain locked if the web process is still referencing the file. In addition the loaded EF context will retain the db name. Ensure the visual studio browser isn't running in the tray still either.

Related

Visual Studio reading Access database

I have written a Windows application in Visual Studio that reads data from a Microsoft Access database. Everything works fine; however, after publishing the application I need it to read the database when the database is changed or updated. Can anyone help with where to put the Access database and how to get the application to look at it when it opens?
It sounds to me like you can place the Microsoft Access (presumably, a single .accdb file) anywhere you want to put it and then--as long as your program knows where that file is--simply use System.IO.FileSystemWatcher.
See this question/answer(s) for more details:
Notification when a file changes?

Prevent accidental update of host database WebMatrix

I use WebMatrix for deployment of files and databases during development. I want to continue to use it after development for updating files on the host. I also want to synchronize my local database to the host database, but never the other way around. I am terrified of accidentally overwriting the database on the host.
How can I do what I want while safeguarding against accidental updates of the database on the host? Essentially, I am looking for a way to tell WebMatrix that the host database is read only and not to be updated.
You can link the development project to the live database. That way you can still develop the files offline while using the actual data to do it. If you link to the database WebMatrix won't attempt to update it as it will already be working with it. Yes, if you make a change for development purposes it will change it on the live site. However, if you are only developing the webpages this should not be a concern. To link to your database just go to the database workspace and click on the New Connection icon. Just remember any changes to the database in WebMatrix after that point will be immediately sent to your database.

Database in Visual Studio's solution

Why does Visual Studio asks (Optionally) to add database .mdf file to be stored in project output folder? It's still is a requirement that .mdf file to be part of running SQL Server instance so that application can work with the database.
For instance if I stop the instance of SQL Server and run the application, it throws exceptions etc. I wonder why it stays in VS solution folders then? Any advantage of this?
I think this is generally to allow for the "User Instance" feature, which lets you make a copy of the MDF file for local debugging purposes (without impacting the database that's running within SQL Server).
You can see this URL for more information on how this feature works, but I would just ignore it, since it is deprecated and in SQL Server 2012 is replaced with a fundamentally better and different way of dealing with isolation and avoiding instance maintenance (no more AttachDbFileName nonsense).
Personally, I think it's much better to work with a single copy of the database, attached to a proper instance of SQL Server, because these other methods just seem far too convoluted and confusing for very little gain. But maybe that's just me.

How can I see sdf file

I am using mvc3 to develop an database application and followed the Code-First Tutorial using Entity Framework 4 of msdn site....
I followed all the steps but unable to see the sdf file....
I can perform CRUD operations but can not see the physical database file inside the app_data folder
I also did "Show all file" but still it is not shown in my visual studio explorer...even my App_data folder is empty...
Code inside the web.config file:
<add name="Forum1Context"
connectionString="Data Source=|DataDirectory|\Forum.sdf"
providerName="System.Data.SqlServerCe4.0"/>
Please Help me!!
I experienced a similar issue. In my case, the name attribute in the add tag was not the same as the name of the class which inherited from DbContext. When I changed the name attribute to match, then I could see the .sdf file, but rows I had inserted previously into the database were lost.
It could be that the Database considered your SQL Express rather then the connection string. Fire up SQL Management Studio and check your SQL Express server
"Data Source=|DataDirectory|Forum.sdf" means Database file is located at "App_Data" folder.
And if you can even perform CRUD operation, Database file should be already there as "App_Data\Forum.sdf". (Please also verify with window explorer).
On Visual Studio, need to enable "Show All Files" on Solution Explorer to view Database file.
Just wanna point out that SQL Express and SqlServer CE are different. SQL Express will need a memory pipe or TCP connection like any other DB server, and the Sql Server CE will automatically create your DB file if doesn't exist at first runtime.
Could it be that you haven't run our app (and EF4.1 code) hence your .sdf file isn't there yet?
Just remove the backslash \ before Forum. I think that will solve it.

MVC Code First: App_Data Folder Not Being Created

I'm using the MVC Code First approach to create a SQL Compact database (from WebConfig: data source=|DataDirectory|MailBoxDB.sdf). The .sdf file should get created automatically, and it does if I manually create the App_Data folder on the web server (Windows Server 2008). However, I'm trying to automate the deployment process and I want to eliminate this manual step. I'm using MSDeploy to create the deployment package. Is it a permissions issue that prevents IIS (7.5) from creating the App_Data folder on its own? If so, which settings should I be focusing on? Any ideas?
I had a similar scenario (generally more complex, although without a database in App_Data). I pieced together several SO questions and found a solution which I've posted on my question on the same topic. Take a look at my answer and hopefully it helps.

Resources