I generated a database through entity framework. Here is the connection string:
<add name="AnnouncementsContainer" connectionString="metadata=res://*/Models.Announcements.csdl|res://*/Models.Announcements.ssdl|res://*/Models.Announcements.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
This uses a file called Database.mdf. The application runs fine but there are two issues that I am having to deal with right now:
Database Updates: Whenever I update the database using raw sql code I have to manually host this file myself in SQL Server Management Studio (I then detach the database and leave the rest to visual studio). The server appears in the Visual Studio server explorer but Visual Studio cannot connect to it to run SQL code. Where is Visual Studio hosting this file so that the application can get to the database but the IDE can't?
Profiling: I have created some stored procedures for entity framework and mapped them to the entities. It would be really nice to see if these entities are actually being executed. How can I see the stored procedures executing with a tool like SQL Profiler?
Here are some screenshots in case what I am saying isn't clear:
SQL Server Management Studio:
Visual Studio Server Explorer:
As you see your connectionstring contains this part:
data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\Database.mdf;
This means that it will take the file Database.mdf stored in your |DataDirectory| (which defaults to App_Data folder in web projects) and it will attach it dinamically to the provided sqlexpress instance (in this case .\sqlexpress which means the sqlexpress instance running on localhost .\ named sqlexpress.
Related
On my PC, I messed up the default installation of the SQL Server that comes with VS22.
I managed to install SQL Server manually again, but I have to write a database class library using EF Core.
For that I have a connection string as follows:
Data Source=(localdb)\\MSSQLLOCALDB;Initial Catalog=TestDB;Integrated Security=True
I've been told that EF will create the database as needed after the name given in the connection string.
This works fine on my new laptop on which I did not touch the SQL Server. On my PC it does not work though.
What do I have to do to get the "default" installation of the SQL Server?
You can install LocalDB through the Visual Studio Installer, as part of the Data Storage and Processing workload, the ASP.NET and web development workload, or as an individual component.
The steps in Visual Studio Installer are as follows:
First select Modity, then select SQL Server Express LocalDB in Installation details. Hope it helps you.
I am new to Visual Studio, but I am very familiar with many different development IDEs.
I am using MS Visual Studio Community Edition 2019, and I have a hosted website with a Ms SQL Server 2016 database.
I've connected to the database successfully within Visual Studio and saved the connection in the Server Explorer as a database connection.
It tests successfully, and I am able to launch a query from the server explorer entry for that connection and select/update/insert etc without problem.
When I open an existing saved SQL file, it does not allow the option to connect to my saved connection in the server explorer. The options under both History and Browse, local/network/Azure.... nothing shows my saved database connection.
Is there something I am not aware of, or that I am doing wrong?
Thanks!
Flynn
Once you open a .sql script in Visual Studio; right click in the editor where you see the SQL code, and go to Connection and then use Connect or Change Connection to change where you are connected/connecting to.
I am trying to attach database file in SQL Server. I am getting the below error. The same error I am getting while trying to attach database from Visual Studio. I am using Visual Studio 2013 and SQL Server 2014 Management Studio.
My connection string:
<add name="Sample"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\M\Desktop\SampleApplication1\App_Data\Sample.mdf;Initial Catalog=SampleDBContext;Integrated Security=True;"
providerName="System.Data.SqlClient"/>
Error:
The database 'sample' cannot be opened because it is version 851. This server supports version 782 and earlier. A downgrade path is not supported. Could not open new database 'Sample'. CREATE DATABASE is aborted.
and here is the default SQL Server instance is set in Visual Studio.
You CANNOT do this - you cannot attach/detach or backup/restore a database from a newer version of SQL Server down to an older version - the internal file structures are just too different to support backwards compatibility. Obviously, your database is from a SQL Server 2016 pre-release (internal version: 852) while the engine you're trying to attach it to is SQL Server 2014 (internal version: 782).
You can either get around this problem by
using the same version of SQL Server on all your machines - then you can easily backup/restore databases between instances
otherwise you can create the database scripts for both structure (tables, view, stored procedures etc.) and for contents (the actual data contained in the tables) either in SQL Server Management Studio (Tasks > Generate Scripts) or using a third-party tool
or you can use a third-party tool like Red-Gate's SQL Compare and SQL Data Compare to do "diffing" between your source and target, generate update scripts from those differences, and then execute those scripts on the target platform; this works across different SQL Server versions.
I was having this issue while following an old Pluralsight MVC4 tutorial.
The course was asking me to use v11.0, but I was running a 2015 version. I opened up the SQL Server Object Explorer and looked at the different versions of LocalDB, and grabbed the name of the one that had my database in it (right-click "Rename").
I then pasted the name in the Add Connection window, under Server Name. Clicked Refresh, and voila, there it was. Hope this helps anyone out there.
This Microsoft link may resolve your problem if you are still running Visual Studio 2015 and seeing this error - the answer above is part of the answer. (megamaiku) The "generate scripts" answer from Mark_S will work assuming you have no trouble mounting/accessing the database. If I recall the SQL Compare is rather expensive but that is a great solution, again, assuming you can mount the database created by your Visual Studio application.
A similar issue arises with Visual Studio 2017 and SQL Server 2017 except you see 852/856 as the version problem.
After moving the database, change your web.config connectionString to match your server; (localdb)\MSSQLLocalDB or possibly localhost depending on what you are running
(SQL 2016/2017/2019).
<add name="DefaultConnection"
connectionString="Data Source=(localdb)\MSSQLLocalDB;myDB.mdf;Initial Catalog=MyDBName;Integrated Security=True"
providerName="System.Data.SqlClient"/>
OR
<add name="DefaultConnection"
connectionString="Data Source=localhost;myDB.mdf;Initial Catalog=MyDBName;Integrated Security=True"
providerName="System.Data.SqlClient"/>
An old article explains how to pull the pieces from the database one at a time and if you're lucky and it still works (it did a decade ago - separate and then put the parts back together), you may be able to get what you need - a newer version restored to an older version. I am unable to find a reference to the trick but rather than scripting your entire database, there is a way to restore down by restoring the header (restore headeronly), then once the header is in place on your server and the permissions and so on are matching, you can restore that database from the newer backup.
However, the generate script is less convoluted and more popular.
I am getting the following error whenever I am trying to open the db_name.mdf file:
The database '{path}\db_name.MDF' cannot be opened because it is version 706. This server supports version 662 and earlier. A downgrade path is not supported. Could not open new database 'db_name.MDF'. CREATE DATABASE is aborted. An attempt to attach an auto-named database for file {$path}\db_name.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Connection string as follows:
<add name="PasswordDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\db_name.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
I am working on a asp.net mvc 4 project within visual studio.
Any help to get the issue resolved are very much appreciated. Thanks.
LocalDB is a new feature introduced in SQL 2012 Express - you will need to install SQL 2012 Express LocalDb if you need to use an MDF in this manner.
Krzysztof Kozielczyk has blogged several entries around on LocalDB here - take note especially of the issues when running under 'Full IIS'.
IMO using LocalDb with ASP.NET is for a developer machine config only - i.e. when you are ready to deploy your (code first?) MVC 4 app to a testing or production environment, you should consider attaching ASPNETDB.MDF more permanently to a SQL Instance.
You cannot attach a MVC 4 localDB in SQL Server 2012 Management Studio. You'll need to connect directly.
Open up SQL Server 2012 Management Studio. You can download a free express version here:
http://www.microsoft.com/en-us/download/details.aspx?id=29062
During installation make sure you check the localDB component to be installed.
After installation, open Management Studio and the "Connect to Server" dialog will appear.
Enter (LocalDb)\v11.0 in the Server Name textbox. Use windows authentication and click connect.
Your asp database will show in the manager. You can then script it out to what ever you want and edit the DB and make the appropriate changes to your connection strings in your web config.
Remember that if you're using Code First, which in VS 2013's MVC Internet template uses, you'll need to check your db creation code in your app start folder.
LocalDB was created so you can create data driven web applications without the overhead of a full version of SQL server running on your development machine. When you deploy to a production server, migrate to a full SQL Server instance.
Hope this helps :)
Set your database connection string to your Locally installed DB is as below.
Web.config :
<add name="ContextName" connectionString="Data Source=ServerName;Initial Catalog=DBCatalogName;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.SqlClient" />
I am trying to learn Asp.net MVC framework. I was looking at the video tutorial at the link below
http://www.asp.net/learn/mvc-videos/video-395.aspx
In this video the very first step is to add a new database to the example application.
I have visual studio installed on my development machine but the SqlServer Express is running on a different machine, so when I try and add a new database following the same steps as the video I get the following error
"Connections to SQL Server files(*.mdf) require SQL express 2005 to funciton properly. Please verify the installation of the component or download from the url"
I am assuming this is because Visual studio is looking for an instance of SQL express on my local machine and since it doesnt exist on the local machine,it errors out.
So how do i tell visual studio, to connect to a different machine and create the database there?
I am using Visual studio 2008 with .net 3.5 Sp1
Make sure that remote connections is enabled in your sql server express installation (http://www.linglom.com/2007/08/31/enable-remote-connection-to-sql-server-2005-express/). Then take a look at www.connectionstrings.com for the various ways to connect to the sql server as appropriate for your environment. Then configure the connectionstrings section in web.config and reference that configuration target when connecting to the db.
Alternatively you might try using LINQ to SQL which will streamline some of these tasks (still need to enable remote connections and get an appropriate connection string defined...LINQ to SQL wizard for the rest).
Open the Server Explorer and add a new Data connection. Same thing as adding a new .mdf. You can use it just the same.
create the database on the remote computer(edit: IIRC, you can also create it within visual studio when you add the connection)
ensure that it allow remote connections
add the connection in database explorer
I'm not sure what's happening in that video, but if you for instance are going to use linq2sql, visual studio will add the connectionstring to web.config when you drag the tables into the .dbml :)
What you'll have to do is attach the mdf to the database server you have running on your separate machine, and then open up your server explorer and navigate to that particular machine and select the database from there.
I am not going to see the video so I can't tell you what step to change. But it should be easy enough to write a connection string (or get it off the web) that connects to a database on the remote computer and not your PC.
I am so used to writing my own connection strings that I don't even know of any option in VS that automates this.
Instead of add a new database to a remote machine as you indicated on your local machine, you can try to create the database on the remote machine firstly, and then connect to that remote database within your visual studio.