Not able to create database using entity framework - c#

I am trying to use code-first approach of Entity Framework in my asp.net mvc application. Below is my connection string which was autogenerated...
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>
Whenever I press F5, the following error occurs:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)
What am I doing wrong??
I have SQL Server 2008 Enterprise edition installed. I am trying to connect to SQL Server using Windows authentication. I am also following a video tutorial so there may not be anything wrong with code

IF you have SQL Server Enterprise edition, you most likely won't have it installed as SQLEXPRESS instance (that's the default for the SQL Server Express installations that come with Visual Studio).
More likely than not, you have the unnamed default instance - so try to use this connection string:
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.;Database=YourDatabaseName;Integrated Security=SSPI;
providerName="System.Data.SqlClient" />
</connectionStrings>
Not sure if your application really uses the ApplicationServices connection string - if not, adapt the one your app is using! Also, you'll need to replace the YourDatabaseName with the actual database name that you're using in your application.

If you are trying to connect to an instance of SQL server that's installed on your machine, you'll want to use a connectionString formatted to use that instead. Your current connectionString is trying to create a database file from which to work, rather than building a database in your SQL server instance.
Try something like this;
connectionString="data source=localhost;user id=<user with dbo access>;password=<user password" providerName="System.Data.SqlClient" />

Related

Migration from local EF database to Google Cloud

Story :
I am trying to create an ASP.NET application hosted on Google Cloud.
This application needs database access to store some data and I am already using Entity Framework to access a local database.
So I am following this tutorial, hoping to find a way to host my database on Google Cloud :
https://cloud.google.com/dotnet/docs/getting-started/using-cloud-sql
But in the Configuring settings's part during the sixth step I get an error.
Configuration :
File : Web.config
<appSettings>
<add key="GoogleCloudSamples:ProjectId" value="area-185910" />
<add key="GoogleCloudSamples:BookStore" value="mysql" />
</appSettings>
<connectionStrings>
<add name="AreaDbEntities" connectionString="metadata=res://*/Models.AreaModel.csdl|res://*/Models.AreaModel.ssdl|res://*/Models.AreaModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\AreaDb.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="LocalMySqlServer" connectionString="Server=IP;Database=NAME;Uid=USERNAME;Pwd=USERPWD" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Error :
Visual studio Error
Website's page's error :
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)]
Question :
Is someone able to explain how to make a database migration from a local EF database (with Entity Framework) to Google Cloud ?
Or is someone able to solve the error ?
Thank you in advance !

ASP.NET MVC : change connection from SQL Server 2012 to SQL Server 2014

I have an existing ASP.NET MVC project which previously was connecting to SQL Server 2012. But now I have only SQL Server 2014 installed and it can not connect.
connectionString="Data
Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspn‌​et-Vidly-20160330105‌​730.mdf;Initial
Catalog=aspnet-Vidly-20160330105730;Integrated Security=True"
My Sql Server works ok, I have version problem, the project is taken from another pc Which had installed SQL-Server 2012 , Now I am trying to run with SQL-Server 2014 installed !!!
It is trying to connect to SQL Server 2012. How can I fix this?
This question may already have an answer here: - I could not find anything helpful.
Looks like there is a problem with your connection string in the application.
This is the standard connection string to connect local DB:
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />
An example with credentials:
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=False;User Id=userid;Password=password;MultipleActiveResultSets=True" />
For more information visit: https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx

C# How to deploy/create setup for VS application with localdb?

Im trying to deploy application application with localdb. What I currently achieved is that I created mdf file from my localdb and add it to setup project (Visual Studio Installer). I modified connection of mdf file to localdb database. What I'm worried about this solution, when I'll edit database and install updates on client PC, if the data from mdf file will be lost. Is there a better way how to deploy or is this method valid?
Problem with this method is that I cant reach my stored procedure, even server explorer shows them.
Using VS 2015, SQL Server 2016 and EF 6 in my app.
Connection string, first one was initial one, connecting right to localdb, second one is connecting to mdf file. First one si working correctly, but only on dev machine, second one is having problems.
<connectionStrings>
<add name="MSDBContext" connectionString="data source=(localdb)\MSSQLLocalDB;initial catalog=MSDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
<add name="MSDBContextFile" connectionString="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\MSDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>

.NET App.config connectionStrings local MS SQL Sever 2012 connection issue

I tried quite many various of the connection strings to get it working as follows:
<connectionStrings>
<add
name="CodeFirstTest"
providerName="System.Data.SqlClient"
connectionString="Server=ma\SQLEXPRESS;Initial Catalog=test;Integrated Security=true;Truested_Connection=true;"/>
</connectionStrings>
Exception message:
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
I'm not sure the exact SQL Server I installed. But by SQL Server Management Studio I can see as properties :-
MA is the name of he pc windows 8.1 installed. Also I tried Server=.\SQLEXPRESS. I get same exception.
Then tried the following:
<connectionStrings>
<add name="Default"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=MA\LOCALDB#57E60DE5;Database=test;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient"/>
</connectionStrings>
I get exception:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified).
Then modified data source=ma\SQLEXPRESS. Still get same error. How do I connect local db in .net / C#?
Change your connection string to be like below (notice the part Server=ma\\SQLEXPRESS;)
connectionString="Server=ma\\SQLEXPRESS;Initial Catalog=test;Integrated Security=true;Truested_Connection=true;"/>
Also notice the part in your connection string Truested_Connection=true;; it should be Trusted_Connection=True;. There is a spelling mistake.
Those keywords much match exactly else an exception would be thrown saying unknown keywords

Connecting to database in Visual Studio Express 2013 for Web

I'm using Visual Studio Express 2013 for web and I'm following this tutorial : http://www.codeproject.com/Articles/791740/Using-AngularJs-ASP-NET-MVC-Web-API-and-EntityFram which specifies this connection to connect ot the db:
<add name="MainDb" connectionString="Server=localhost; Database=SimpleTaskSystemDb; Trusted_Connection=True;" providerName="System.Data.SqlClient" />
but when I run Update-Database I get this error:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
I've also finished the Music Store app which is working fine and the connection there is different:
<add name="MusicStoreEntities" connectionString="Data Source=|DataDirectory|MusicStore.sdf" providerName="System.Data.SqlServerCe.4.0"/>
I tried a similar connection string but that didn't work either.
No Entity Framework provider found for the ADO.NET provider with
invariant name 'System.Data.SqlServerCe.4.0'. Make sure the provider
is registered in the 'entityFramework' section of the application
config file.
However the Music Store app.config file does not reference System.Data.SqlServerCe.4.0 but I added it to this app anyway and then I get back to my original error.
Any help would be greatly appreciated.
Here are some of the sample connection strings I have described every connection string with full details
For Local Sql server (BuildIn Sql Server Express into Visual Studio).
For Sql Server instance which comes default with visual studio you can use following connection string
<connectionStrings>
<add name="ConnectionStringName" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=YourDatabaseName;Integrated Security=True"
providerName="System.Data.SqlClient" />
For Sql Server Which Has been installed seperately such as Evaluation Edition etc
<connectionStrings>
<add name="ConnectionStringName" connectionString="Data Source=YourPCName-PC\ServerInstanceName;Initial Catalog=YourDatabaseName;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
For CompactEdition of Sql server you can use provider name as
providerName="System.Data.SqlServerCe.4.0"

Categories