Cannot connect to Database with Entity Framework - sql-server

Every time I have a fresh install of Windows, Visual Studio and Sql Server I always have problems getting an application to connect to the local instance of Sql Server.
I am trying to start an application using Entity Framework code first. Here is my connection string.
<add name="KCSoccerDataContext"
connectionString="Data Source=.\MSSQLSERVER;Initial Catalog=KCSoccer;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
I am able to connect to the database instance using Sql Server Management Studio, but can't seem to connect using this connection string. I am assuming the problem has to do with how the database instance is configured.
The error I'm getting is
{"The provider did not return a ProviderManifestToken string."}
I'm not exactly sure what this means or where to go from here.
Any help anyone could give on this would be awesome.
Thanks

Use just this connection string:
<add name="KCSoccerDataContext"
connectionString="Data Source=.;Initial Catalog=KCSoccer;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
MSSQLSERVER is name of default instance. It is not used in connection strings. It even doesn't work from Management studio if you try using it. Only names of custom instances must by used.

You probably didn't enable remote connections for SQL Server since it's a fresh installation. Follow first solution here or here.

Related

Can't connect SQL Server 2016 Developer via connection string generated by .edmx file

I'm working on a project using SQL Server 2008 Express. I had an instance called SQLEXPRESS and I used to connect to the DB via this connexion string :
<add name="MyConn" connectionString="metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;provider=System.Data.SqlClient;
provider connection string="data source=PCNAME\SQLEXPRESS;initial catalog=DbName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
This connection string was generated by .edmx file (I'm working on an ASP.NET MVC project, database first, and Entity Framework) and it worked fine.
I recently installed SQL Server 2016 with a Developer Licence. During the install process, I used the default instance, which is MSSQLSERVER. For information, the generated InstanceID is MSSQL13.MSSQLSERVER.
I have a SQL project in my solution to regenerate my tables on my new SQL Server; I could connect easily to the server via the wizard.
I also used SQL Server Management Studio to verify everything was fine on the server, and it works perfectly (tables are generated, connection was OK).
However, my code is unable to connect to the server and systematically failed to the first call with error 26 - can't locate server instance
Here is what I've tried so far:
Change the data source parameter of the connexion string to localhost , ., .\MSSQLSERVER, .\PCNAME, PCNAME.MSSQLSERVER, PCNAME.MSSQL13.MSSQLSERVER, DOMAIN.MSSQLSERVER, DOMAIN\PCNAME => Doesn't work
Verify SQL Service are running => They are
Create an .udp file, connect to my server, and copy/paste the generated connection string : Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MyDB;Data Source=PCNAME => Doesn't work
Regenerate my .edmx file (and the connection stirng) => Doesn't work
Verify this suggestion => It was already correct
Why does all the connection wizards seems to work (SSMS,
.udl file, .edmx generator, my DB project), whereas my code is unable to reach the server? I didn't change any line of code.
EDIT : The crazy thing is I have a log database on the same server, the connexion string is
<add name="NLogConn" connectionString="Data Source=localhost;initial catalog=DBLOG;integrated security=True" providerName="System.Data.SqlClient" />
And it is actually logging the network error !! So is it really a connection string error or an EF error ? I'm using the latest EF version, 6.1.3 on each project referencing EF (The main and the DAL layers)
Before going off the deep end on what it could be let's start with a working connection string I just tested on my local machine on Visual Studio 2017 Enterprise with Entity Framework 6.1.3.
<connectionStrings>
<add name="TesterEntities" connectionString="metadata=res://*/Tester.csdl|res://*/Tester.ssdl|res://*/Tester.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Tester;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
The marked up connection string is:
="data source=.;initial catalog=Tester;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"
For my test connection I am using my local server for a default instance also of 2016 developer edition(. is abbreviation for this). I am using integrated security and my database name(initial catalog) I am targeting is Tester.
There are a few things to consider if Entity Framework(EF) is not properly hitting a target.
What version of EF are you using? Before MS moved to Core, I believe the last .NET full edition of EF was 6.1.3. Ensure you are on the most current version on Nuget download.
If you go from Express to a full fledged SQL the connection string's 'provider' often will not be the same I believe. Or it could be the local default instance before sql is installed is different. Ensure you don't overwrite an existing connection string with this. The provider should be: 'provider=System.Data.SqlClient'
You are referencing a project of EF in ANOTHER project. Each project that references EF should be having the NuGet package for EF as well as a config file with the connection string. EG: I have a console project for testing things I write and reference another project called 'EF Testing' and I don't have an app config with a connection string. It won't work.

How can I change my connection string to point to SQL Server instead of SQLEXPRESS?

created a project with Web Developer Express 2012 using SQLEXPRESS as the database. I’ve deployed to a shared hosting site and I’m trying to use the connection string provided to me. I was given this:
Data Source=tcp:TheDatabase.com;Initial Catalog=TheNameOfTheDatabase;User ID=Username;Password=******;Integrated Security=False;
Here is how I’m using it:
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=. TheDatabase.com;Initial Catalog= TheNameOfTheDatabase;User ID=Username;Password=******;Integrated Security=False; ;User Instance=true"
providerName="System.Data.SqlClient" />
I get this error message:
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)
All the research I did points to my connection string trying to access an instance of SQLEXPRESS only I don’t see any where in my string where this is happening. How do I make it point to SQL Server instead of SQLEXPRESS?
Is there somewhere else in web.config that may need changing?
I know this would be easy to fix if I could use IIS to manage the database but my ISP is blocking port 1433 and I can’t connect directly to the site’s database. Very frustrating.
UPDATE"
I think my problem is in the Computername/Instance section of the string. Can someone please tell me which part of my string would be the computername and the server instance? On my local machine it is MyPcName/SQLEXPRESS or .\SQLEXPRESS.
UPDATE: Ok got an update from the winhost forum. I shouldn't be using the server_name/Instance_name anyway so I am at a lost.
So my project was using the default database (SQL Express) even though I didn’t have a connection string pointing to it. The EF uses convention to attach the default database if you don’t specify by using a constructor like this:
public class BloggingContext : DbContext
{
public BloggingContext()
: base("BloggingDatabase")
{
}
}
Then this:
<configuration>
<connectionStrings>
<add name="BloggingCompactDatabase"
providerName="System.Data.SqlServerCe.4.0"
connectionString="Data Source=Blogging.sdf"/>
</connectionStrings>
</configuration>
I found it all here…
http://msdn.microsoft.com/en-us/data/jj592674.aspx.

Cannot create a local sql server 2012

I am trying to create a database for an MVC 4 application. I used Entity Framework Code first approach. After digging into the problem I realized that it was not a connection string issue. I downloaded Sql Server Data tools and tried to create it from there but I get the same exact error which is related to Windows Authentication. I am not sure what is causing this problem, I even tried running as admin.
Any ideas??
I have tried mostly all forms of connections strings available online, the last two that I tried were
<add name="Request" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Requests;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Requests.mdf" providerName="System.Data.SqlClient" />
<!--<add name="RequestsContext" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|\Requests.mdf" /> -->
I am trying to create a local database, I have asked a question about entity frame work right here thinking that the problem was from there but now I know it has nothing to do with it. Here you can see all of the details about the models I created and I am trying to generate the database from.
Using Entity framework with SQL Server 2012 (local *.mdf database)
You don't have SQL Server installed on your PC. Install Express Edition and you should be fine.
EDIT: Use connection string for local server:
<add name="Request" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=Request;Integrated Security=True;" />
If its local, probably SQL Server service is not running. Start>cmd and services.msc will open services page. Right click on sql server and start

How can I view the data in an SQLEXPRESS DB which is connected in Web.config?

I'm running Visual Web Developer 2010 Express. I have added an additional SQLExpress DB to the standard ASPNETDB.mdf. Both are connected through the web.config. Everything runs fine. The connection strings are as follows:
<add name="TrempimModel"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|TrempimModel.mdf;
database=TrempimModel;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;
User Instance=true"
providerName="System.Data.SqlClient" />
But how do I view the data in both DB files? To my surprise, although the files are refferenced in web.config, the DB Explorer in the IDE does not show them. If I go and add either or both in the DB explorer, strange stuff starts happening - mainly the application is not able to connect to my added DB at application launch.
So what is the best way to view SQL Express table data? Is it through the IDE, or a seperate application? And why isn't the DB explorer showing a connection established in web.config?
Thanks,
Gil
Problem Solved - surprisingly enough, by eliminating the connection sting altogether. That way, the database is created by the DB engine and attached automatically by Management Studio, so the data can be viewed over there, while debugging.
I wander why all the walk-through descriptions eliminate this option which is as hassle free as they come... One point to remember is that you need to exit the Management Studio before running the application with data model changes that will cause the DB to be re-created.

sql server express connection string

Is there a difference between sql server connection string and the express vesion?!
By default (though I don't recommend it1), Sql Express is installed as a named instance. That means you must connect like Server=servername.com\SQLEXPRESS as opposed to just Server=servername.com.
As always, connectionstrings.com has the goodies.
1 I don't recommend named instances because they run on a different port, which makes firewalling a PITA. Also, it makes little sense to me to run more than 1 MSSQL Server on a computer when you could just host multiple databases instead - so I'm not really sure what the point is.
Yes there is a difference- the big one being you won't have AttachDbFilename in the full SQL Server.
SQL Server Express connection string:
Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
Typical normal SQL Server connection string:
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
See connectionStrings.com for more info.
The connection string used to access SQL Server and SQL Server Express?
Generally, no. That's the nice thing about using Express; develop with it locally and with minimal changes migrate your data access to SQL Server.
The only difference is that you'll have to specify a named instance in the conn string, if Express was setup that way. 2005 had no option, but 2008 does.
Data Source=localhost\SQLExpress
Here's a great reference for connection strings.
I recently downloaded a sample application from the Telerik site and could not get it to run on my machine that has only SQL Server 2008 SP1 Developer Edition installed until I did a find and replace of all "User Instance=True" with "User Instance=False".
Before anybody goes and gets their panties all twisted in a knot, hear me out first. My circumstances may be (but ought not be) unique in that instead of installing SQL Server 2008 Express just to run example applications (e.g., Telerik), I simply installed a second named instance of SQL Server named, you guessed it, sqlexpress on my copy of SQL Server 2008 Developer Edition. That way when the Telerik (or you name it) demo application tries to connect to "Data Source=.\sqlexpress", it's none the wiser that what it's really connecting too is "big SQL Server" (which is what SQL Server Developer Edition is for the purposes of this posting).
The key conecpts (dare I say "money quote") you need to understand are these:
SQL Express allows (actually assumes if missing) User Instance=True, so if you are running a named instance called sqlexpress on "big SQL" (Developer, Standard, Enterprise or Data Center), then you just need to find/replace User Instance=True with User Instance=False in the connecton string and your worries are over. Even the "Attach database" stuff (that I see in the Telerik connectionStrings section of their web.config) works on "big SQL" (contrary to what somebody posted earlier in this thread).
Here's an actual example taken directly from the web.config of the Telerik web.config file after I "fixed it up":
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Northwind.mdf;Integrated Security=True;User Instance=False" providerName="System.Data.SqlClient"/>
<add name="TelerikConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Telerik.mdf;Integrated Security=True;User Instance=False" providerName="System.Data.SqlClient"/>
<add name="NorthwindConnectionString35" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Northwind.mdf;Integrated Security=True;User Instance=False" providerName="System.Data.SqlClient"/>
<add name="TelerikConnectionString35" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Telerik.mdf;Integrated Security=True;User Instance=False" providerName="System.Data.SqlClient"/>
<add name="NorthwindEntities" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Northwind.mdf;Integrated Security=True;User Instance=False;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
<add name="TelerikEntities" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Telerik.mdf;Integrated Security=True;User Instance=False;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
<add name="EditorFileBrowserDatabase" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|EditorDBStorage.mdf;Integrated Security=True;User Instance=False" providerName="System.Data.SqlClient"/>
</connectionStrings>
In addition to previous post: If you have troubles with setup Telerik samples on MSSQL Server 2008, you can do the following:
Open LiveDemos Telerik VS solution
Setup database connections for telerik .mdf files in VS master
Copy connection string for connections to the web.config file
And add "Integrated Security=True;User Instance=False" to the end of the strings.
PS. Fred Morrison - thanx, you are my "man of day", I just fix Telerik Live Demo problem with you post help!

Resources