I've created an entity model file (.edmx) based on an .mdf file in my asp.net application, placed in my App_Data Folder.
first of all my connection string, created by the entity framework wizard :
<connectionStrings>
<add name="Sales_DBEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=.\SQLEXPRESS;attachdbfilename="c:\users\ext\documents\visual studio 2010\Projects\WebProject_A\WebProject_A\App_Data\Sales_DB.mdf";integrated security=True;USER INSTANCE=TRUE;multipleactiveresultsets=True;App=EntityFramework'"
providerName="System.Data.EntityClient" />
</connectionStrings>
The above is placed in an app.config file created by the wizard.
I've copied the same connection string to the web.config file as well (I'm not sure if this is necessary).
I've come across two problems when attempting to later run the same application on a my machine (I've created the application on a different machine).
The first was regarding the user instance:
Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed
The first thing I don't really comprehend is what a user instance actually defines.
According to MSDN :
A user instance is a separate instance of the SQL Server Express Database Engine that is generated by a parent instance
Is my local database considered a user instance ?
I would really like some clarification on the matter because I came across posts suggesting
to mark it as False, and the reason was not clear to me .
another post on this matter
From there I came across another solution that explains that for this problem you need to
enable user instance in the SQL Server on your machine.
The Fix
I've done what was explained in that post
but now I've come across a new problem :
An attempt to attach an auto-named database for file (Full Path to my app_data\my.mdf file) A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
I've already attempted to delete all the files under
C:\Documents and Settings\(your user account name)\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS
and rebooting like suggested in other posts, but still I can't find a clear solution for this problem.
I know this is a long post but any help would be appreciated
thanks in advance.
Read this very good overview of what user instances really are.
In a nutshell:
SQL Server allows you to have multiple instances of itself on a single computer - the default instance can be accessed using just the machine name (or IP address) of the host machine - all other instances need to be specified by an instance name (like the SQLExpress on your machine)
for development purposes, you can also create a user instance - each user gets their own separate instance of SQL Server (works in the Express edition only) and a database is attached by its file name (path\Sales_DB.mdf) to that user instance. This instance is started up on demand, then runs and is shut down when no longer needed
While this works great for development, it's not really intended for later on - certainly not for production use.
In a production environment, you want to attach your database files to the actual SQL Server instance they should run on - typically using SQL Server Management Studio. When you do this, you no longer refer to that database via its file name, but instead you use the database name that was given to the database when being attached to the server. In this case, SQL Server will handle all details of operations - and that SQL Server instance is up and running all the time (doesn't need to be started "on demand" all the time), and you can use things like permissions and logins to control access to the database.
Your connection string would then look something like this:
<connectionStrings>
<add name="Sales_DBEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='server=YourServerNameHere;database=Sales_DB;integrated security=True;App=EntityFramework'"
providerName="System.Data.EntityClient" />
</connectionStrings>
When you have attached a database to a production (or testing) SQL Server instance, you do not need to know what the files are that make up the database, and you don't need to specify those files - SQL Server will take care of that. You just refer to the database via its database name.
The solution is always simpler then anticipated:
double click the model.edmx file -> the designer will open.
right click on the designer -> Update model from database .
new connection -> Choose Database file (.mdf) under type .
Browse -> go to your App_Data folder, and choose the DB.
copy the connection string created in App.config to web.config.
bye.
Related
I am following this tutorial (http://www.asp.net/mvc/overview/getting-started/database-first-development/setting-up-database) in order to create a database-first app with Entity Framework.
I hit "Run" and everything seems to be working, but in the next part of the tutorial (https://www.asp.net/mvc/overview/getting-started/database-first-development/creating-the-web-application), when I try to add a connection, it's not in the drop-down menu in the "Connection Properties" box under "Select or enter a database name".
Which server is the database on? I didn't specify anything about the server when I created the project for the first tutorial, and I figured that it would be on the default LocalDB that comes with Visual Studio (server name: (localdb)\MSSQLLocalDB), but the database isn't there. Any ideas where I should look or what to try?
Maybe use . (a dot) as Server Name?
Open your Web.config file and check your <connectionStrings> this is where the data source of the database will be. As shown:
However, which version of SQL Server do you have? As this can determine what the correct connection string will be.
Hit Windows+R. And run the following:
%LOCALAPPDATA%\Microsoft\Microsoft SQL Server Local DB\Instances
You may have multiple instances, it would be worth checking each one. So it would be:
(localdb)(name of instance)
This article may also be of use to you:
http://rajchel.pl/2015/10/solving-connection-issue-with-sql-server-2014-localdb/
What could be the case is that you have to run the application you created, and then do something to touch the database like creating / registering a user. After registration you can find the database on the specified location in your web.config settings. This was the solution in my case.
I am taking over the maintenance of a website. Virtually no documentation. I hope that this kind of question is "ok" here ...
Right now, I am trying to locate/view the database but I can't find it.
This is the only connectionString in web.config:
<connectionStrings>
<add connectionString="Data Source=YPOC\SQLEXPRESS;Database=Marco;User ID=yyy;Password=xxx;" name="DefaultConnectionString" />
</connectionStrings>
QUESTIONS:
1) are these parameters equivalent?
a) Database=Marco
b) AttachDbFilename=|DataDirectory|\Marco.mdf
c) Initial Catalog=Marco
2) if the answer to #1 is "yes", then I should find a database file called Marco.mdf in the App_Data folder, right?
3) but #2 can't be correct, because I do not see a database file called Marco in the App_Data folder ... ;-x
4) where in the world is carmen sandi ... oops, I mean Marco.mdf ?!
RELATED QUESTIONS:
I had to copy all the files from a production server called YPOC. In the web.config, I think I need to change "Data Source=YPOC\SQLEXPRESS" ... my SQL Server Instance Name is "SQLEXPRESS" ... therefore, should I change the DS to "Data Source=.\SQLEXPRESS"?
In the App_Data folder, I found ASPNETDB.MDF ... in Server Explorer, I try to connect to view the tables, but I get an error message:
"...cannot open because it is version 661. This server supports version 655 and earlier"
I did some research and it appears that version 661 means that it needs SQL Server 2008 R2 ... but I have that installed already ... ;-(
I'm used to a shared hosting environment where the SQL Server DB is set up and it's easy to connect. Here, it is on a dedicated cloud server (unmanaged) and I'm been thrust upon new territory where I can't even find the database ... ;-(
Any help is greatly appreciated ...
nano
Stop, stop, stop, stop, stop using the User Instance and AttachDbFileName nonsense. Please. You will be much more sane if you forget they exist. I'm totally serious. The user instances feature has been deprecated for a reason.
Take your MDF file, which you can find by searching using Windows Explorer (if you've been using this user instance feature, you will probably find multiple, and I have no suggestions for determining which one is the right one). Once you've found the one you want to use, attach it to a proper instance of SQL Server using CREATE DATABASE Marco ... FOR ATTACH_REBUILD_LOG. Your connection string should then point to your server (yes, .\SQLEXPRESS should be fine if you are connecting locally), and should refer to your database by logical name (e.g. Marco, no path information to a specific MDF file).
Also you'll want to install Service Pack 1 for SQL Server 2008 R2. It seems like you created a database in a post-RTM build of SQL Server 2008 R2 (or in 2008 with SP2+).
"But how do I access this database from my dev computer when I don't know the SQL server address, etc?"
I found the Marco database. It is on SQL Server that is located on an unmanaged web server account. The way to access the database is to use a Remote Desktop Connection, then use SQL Server Management Studio that is located on the server (not my dev desktop). The SSMS then found the server/database and voila, connected ... my confusion was in trying to connect using SSMS from my dev desktop directly instead of using SSMS located on the server via RDC ...
I am working on visual studio 2010 and i have a database in Server Explorer that i can see that is not in the App_data in the solution explorer.. I need to copy it somewhere to upload it to the host..
I think the database is suitable for SQL server2005 , cause that is the only sql server version installed on my computer with the vs2010.
Here are my connection string:
<add name="YourGuruDB" connectionString="Data Source=DIMA-00AA1DA557;Initial Catalog=model;Integrated Security=True" />
You can back up the database by right click on db and choose back up. Restore it on your host /web server.
I would prefer to export you database schema and mandatory data as sql scripts using SSMS (SQL Server Management Studio) then run the script on the host.
Here is a great tutorial about it http://blog.sqlauthority.com/2007/08/21/sql-server-2005-create-script-to-copy-database-schema-and-all-the-objects-stored-procedure-functions-triggers-tables-views-constraints-and-all-other-database-objects/
EDIT: if you are using VS2010 there is a new feature in the deployment called "Package/Publish SQL" allow you to package your SQL DB. Here is a tutorial how to use it http://rachelappel.com/deployment/database-deployment-with-the-vs-2010-package-publish-database-tool/
Erm. your connection string says the database in use is model. That's a special database in SQL Server that's used a template for new databases (that's why you can't detach/copy). You're going to have all sorts of problems if you stick with that name.
But DO NOT rename it. You're going to have to create a new database - which will have everything you've put in model in it. Then remove everything you've added to model.
You should now be able to detach your new database and move it elsewhere.
Select you database in SQL Managment Studio.Then Tasks\Detach.
After this you can copy the database files to another distination. To make you database on-line again, just do Tasks\Attach.
P.S It's only the one among other approaches to accomplish your task.
I have a ASP.Net program written in C# that uses a database stored in the AppData directory, the program works perfectly on my development (Win7\SQL 2008R2) when I deploy to a Win2008R2\SQl 2008 R2 server the program is unable to connect to the database. I'm using the connection string below. Several sites suggest I set the Application Pool to use 'Network Service' as this has Modify permission to the AppData directory, I have done this but it still won't work, another site suggested I Application Pool to "Load User Profile = True" but still no joy.
I'm pulling my hair out with what else to try, can anyone help?
data source=.\SQLEXPRESS;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|\DB.mdf;User
Instance=true
I get "Object reference not set to an instance of an object." in my app and "An attempt to attach an auto-named database for file C:\inetpub\wwwroot\ADPhonebook\App_Data\DB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share." in the Application Event Log
I really appreciate any advice, thanks.
Steven
As Martin points out in the comments, it would be interesting if you posted the exact error message (and maybe the full exception detail) you are getting.
But problems like this have mostly to do with rights at the OS level.
So make sure that the user (identity) of the Application Pool has full access (ntfs) rights to the .mdf file, and try again.
Edit (after you posted the exact message):
Ok, I guess this means that on the server you're deploying to, there is already a database called DB on that Sql Server instance. So you will have to remove or rename that one in order to be able to attach a database with that name.
I think the user rights problem is also still possible though.
Since the answers did not really cover the problem, I posted this on ASP.Net and completly rephrased it. I will post the edited question here:
I have been using the attachDB connection string and I usually deploy to IIS. The site works fine, however, I made some changes to the database and the newest version would not copy as it said file in use.
I opened up SQL management studio and saw that it was mounted so I did a dettach.
I was then able to copy the new version without problem, however, when I next run the site, I get:
Unable to open the physical file "C:\inetpub\wwwroot\vs\App_Data\aspnetdb.mdf". Operating system error 5: "5(failed to retrieve text for this error. Reason: 15105)".
An attempt to attach an auto-named database for file C:\inetpub\wwwroot\vs\App_Data\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
And, if I try to access .Net users or the other few options within IIS Manager, I get the following error:
.NET Users
There was an error while performing this operation.
Details:
A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)
OK
I have tried recycling the application pool, restarting the SQL instance and even restarting the computer.
Nothing helps and I cannot figure out what is wrong... Where does it remember where previous databases are connected and why doesn't it automatically reattach the database?... Someone said that they auto detach after 2 1/2 hours, however I waited 5 hours when a database wasn't in use and SQL Manager showed it was still attached.
When I manually reattach the database, everything works fine.
When you ask a database to be attached on-the-fly to a SQL Express instance using the AttachDBFileName connections string the application will not connect to the SQL Expres sinstance at all, but instead it will connect to a child instance, which is an new instance created specificaly for the user requesting the attach operation. See SQL Server 2005 Express Edition User Instances. This child instance will attach the database and will continue to run for up two one hour, after which it will shut itself down.
When you try to connect from 'enterprise manager' you will not be able to connect to the child instance (is realy complicated to connect explicitly to one, so you cannot accidentaly do it), you are connecting to the parent instance and messing with the database.
To summarize, either stick with the RANU model and use AttachDBFileName, or use a normal database operations mode and manage the database from the SSMS. Don't mix the two.
Are you certain there isn't already a database with the same name attached to the SQL Express instance you are pointing your site at?
Also, does the identity the site is running under when it attempts to attach the database have admin rights in SQL Express? If not, this command won't work (I'm not sure what error it gives in that situation, but "Access Denied" would sound reasonable).
The page "SQL Server 2005 Express Edition User Instances" seems to give a good overview of the issues and workarounds.
Edit to add
Could this be the issue:
An error will be generated if a log file exists in the same directory as the data file and the 'database' keyword is used when attaching the primary data file. In this case, remove the log file. Once the database is attached, a new log file will be automatically generated based on the physical path.
Taken from the documentation for SqlConnectionStringBuilder.AttachDBFileName Property
You'll need to delete the log file before you can re-attach the database through this method.