Connection to SQL server with EnitityFramework 6 code first - sql-server

Okay my project has a separate class library that has all the EnitityFramework version 6.1.3 code and settings.
I can not get this to connect to anything other than my local sql db
I can connect to my sql box 192.111.111.111\sql_box with SQL manager, SQL server object explorer and LINQpad. so it is there and connection string is correct.
I think I am now meant to be doing code based configuration
so my App.cong looks like this
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<entityFramework codeConfigurationType="MyProject.CodeConfig, MyProject">
</entityFramework>
</configuration>
and I have also create and class that derives from DbConfiguation
public class CodeConfig : DbConfiguration
{
public CodeConfig()
{
this.SetProviderServices("System.Data.SqlClient",
System.Data.Entity.SqlServer.SqlProviderServices.Instance);
}
}
but where do I put 192.111.111.111\sql_box
every time I run Update-Database in the package manager I get
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)

In the web.config or in the app.config.
<configuration>
<connectionStrings>
<add name="YOURDBCONTEXTNAME" connectionString="data source=SERVER;initial catalog=DATABASE;Integrated Security=false;persist security info=True;user id=USER;password=PASSWORD;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

Your connection string needs to be in the web/app config file in the startup project.
The connection string in the class library (assuming it is not the startup project) is used when making model changes, migrations, etc.

Related

SQL connection string from ASP.NET app does not work in WPF

Several ASP.NET applications are running on the server of our company.
Connections strings in web.config look like this:
<add name="somename"
connectionString="Data Source=SERVER\INSTANCE; Initial Catalog=DbName;User Id=user; Password=*******; MultipleActiveResultSets=True; Integrated Security=true"
providerName="System.Data.SqlClient"/>
and work perfectly with no errors.
But when I copy this connection string from web.config to app.config of my WPF app, I get the following errors:
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
Or (if I remove Integrated Security from string)
Login failed for user 'username'.
Also I cannot log in via SQL Server Management Studio (causes the same errors).
My app.config looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</configSections>
<connectionStrings>
<add name="somename"
connectionString="Data Source=SERVER\Instance;Initial Catalog=DbName; User Id=user;Password=******;MultipleActiveResultSets=True;Integrated Security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
Both username and password are 100% correct, SQL Server uses mixed authentication.
Managed to log in with sa login of SQL Server
Edit the registry using regedit. (Start –> Run > Regedit )
Navigate to: HKLM\System\CurrentControlSet\Control\LSA
Add a DWORD value called “DisableLoopbackCheck”
Set this value to 1

Change connection string in Entity Framework from localDB to Microsoft SQL Server

I'm trying to use SQL Server 2016 instead of localdb. When I try to change it in the web.config file from mssqllocaldb to v13.0
it gives me the following error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code.
Additional information: 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
You can read about the configuration at MSDN: Entity Framework Config File Settings
An example web.config could be:
<?xml version="1.0"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=MySqlServerInstance;Initial Catalog=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>

Visual Studio 2015 cannot connect to SQL Server LocalDB

I'm just starting to learn C# and Entity Framework 6 ... and it's already far worse than I expected.
I have created a simple new project, and added EF6, while creating to database with Update-Database, I get:
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 haven't changed any config or anything, just created some classed and wanted to generate the database, here is the default connection factory:
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="MSSQLLocalDB" />
</parameters>
</defaultConnectionFactory>
I even checked if the instance is running by running this command:
sqllocaldb info
which resulted in:
MSSQLLocalDB
ProjectsV13
and same for this:
SqlLocalDB.exe start
LocalDB instance "MSSQLLocalDB" started.
I have even tried to disable the firewall ... but nothing.
EDIT:
App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="MSSQLLocalDB" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
after i saw your project you just need change the app.config connectionStrings
<connectionStrings>
<add name="masterEntities"
connectionString="data source=(localdb)\MSSQLLocalDB;initial catalog=master;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework;"
providerName="System.Data.SqlClient" /></connectionStrings>

Error: 26 - Error Locating Server/Instance Specified (Tried many things but couldn't find solution)

This question is already on Stack Overflow. But nobody solve my issue.
When I open my webpage (on my uploaded website on server) in which I am using database connectivity below error comes -
http://puneetchawla-001-site1.smarterasp.net/blog.aspx
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)
Searched on Google and found many thing and applied it but no one solved my issue.
Windows service called "SQL Server Browser" and start the service (I have enabled it).
Enabled incoming port TCP 1433 ( I have done this).
My web.config file -
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<connectionStrings>
<add name="myWebsite" connectionString="Data Source=.\sqlexpress;Initial Catalog=myWebsite;Integrated Security=True; " providerName="System.Data.SqlClient" /> </connectionStrings>
<system.web>
<customErrors mode="Off" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
I have uploaded my website here - http://www.smarterasp.net
and their connection string samples for MSSQL are below-
"Data Source=SQL5014.Smarterasp.net;Initial Catalog=DB_9D58DA_myWebsite;User Id=DB_9D58DA_myWebsite_admin;Password=YOUR_DB_PASSWORD;"
I did changes in web.config based on our samples but didn't get solution.
I think, there will be only changes in web.config and rest all are correct.
You had not written the connection string (as specified in sample) in web.config file.
Your web.config file should be like
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<connectionStrings>
<add name="myWebsite" connectionString="Data Source=SQL5014.Smarterasp.net;Initial Catalog=DB_9D58DA_myWebsite;User Id=DB_9D58DA_myWebsite_admin;Password=YOUR_DB_PASSWORD;" /> </connectionStrings>
<system.web>
<customErrors mode="Off" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
Just change your web.config file and you will get the solution.

SQL Server Project Automate Unit Testing

I have a SQL Server (2012) project in VS2013. I also have an app.config with Local configuration and [tfsbuildserver].sqlunittest.config with server connection string and a relative path.
When performing a check-in executes a build definition that makes deploy and run the tests. The deploy done correctly, but when tests throws me the following error:
An error occurred while SQL Server unit testing settings were being read from the configuration file. Click the test project, open the
SQL Server Test Configuration dialog box from the SQL menu, add the
settings to the dialog box, and rebuild the project.
app.config:
<configuration>
<configSections>
<section name="SqlUnitTesting" type="Microsoft.Data.Tools.Schema.Sql.UnitTesting.Configuration.SqlUnitTestingSection, Microsoft.Data.Tools.Schema.Sql.UnitTesting, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<SqlUnitTesting AllowConfigurationOverride="true">
<DatabaseDeployment DatabaseProjectFileName="[RELATIVEPATHLOCAL]"
Configuration="Release" />
<DataGeneration ClearDatabase="true" />
<ExecutionContext Provider="System.Data.SqlClient" ConnectionString="Data Source=[LOCALSERVER];Initial Catalog=[DATABASE];Integrated Security=True;Pooling=False"
CommandTimeout="30" />
<PrivilegedContext Provider="System.Data.SqlClient" ConnectionString="Data Source=[LOCALSERVER];Initial Catalog=[DATABASE];Integrated Security=True;Pooling=False"
CommandTimeout="30" />
</SqlUnitTesting>
</configuration>
[tfsbuildserver].sqlunittesting.config:
<SqlUnitTesting>
<DatabaseDeployment DatabaseProjectFileName="[RELATIVEPATHTFS]"
Configuration="Release" />
<DataGeneration ClearDatabase="true" />
<ExecutionContext Provider="System.Data.SqlClient" ConnectionString="Data Source=[SERVERTEST];Initial Catalog=[DATABASETEST];Persist Security Info=True;User ID=[USER];Password=[PASS];Pooling=False"
CommandTimeout="30" />
<PrivilegedContext Provider="System.Data.SqlClient" ConnectionString="Data Source=[SERVERTEST];Initial Catalog=[DATABASETEST];Persist Security Info=True;User ID=[USER];Password=[PASS];Pooling=False"
CommandTimeout="30" />
</SqlUnitTesting>
Tests run correctly locally. The error occurs when performing the build definition
Sorry for my English.
Thanks
Turns out the issue was that I had leading white space before the <SqlUnitTesting>, once these were removed the test ran as expected and remove
<?xml version="1.0" encoding="utf-8" ?>
URL: Link Resolved

Resources