Entity Framework code first approach connection string configuration - c#

I've implemented Identity & OWIN authentication to my existing asp.net web form application. I referred to the site:
https://learn.microsoft.com/en-us/aspnet/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project
I am having problem with connection string for entity framework code first approach. I referred to the site: http://odetocode.com/Blogs/scott/archive/2012/08/14/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx for help. But none of them is working.
I use VS 2015, SQL Server 2014 & Windows 10 OS.
For SQL Server authentication (Windows authentication) my server name is : DESKTOP-07C2G55.
I changed my connection string as told in the sites but none of them worked. My first connection string is::
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=DESKTOP-07C2G55;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
On using this connection string when I try to register any user I am getting error as:
Directory lookup for the file "c:\users\myPC\documents\visual studio 2015\Projects\RENTAL\RENTAL\App_Data\WebFormsIdentity.mdf" failed with the operating system error 5(Access is denied.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Directory lookup for the file "c:\users\myPC\documents\visual studio 2015\Projects\RENTAL\RENTAL\App_Data\WebFormsIdentity.mdf" failed with the operating system error 5(Access is denied.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Then I run the following commands in my VS console. I get:
PM> Get-Service | Where-Object {$_.Name -like '*SQL*'}
Status Name DisplayName
------ ---- -----------
Running MSSQLFDLauncher SQL Full-text Filter Daemon Launche...
Running MSSQLSERVER SQL Server (MSSQLSERVER)
Running MSSQLServerOLAP... SQL Server Analysis Services (MSSQL...
Stopped SQLBrowser SQL Server Browser
Stopped SQLSERVERAGENT SQL Server Agent (MSSQLSERVER)
Running SQLWriter SQL Server VSS Writer
PM> SqlLocalDb info
MSSQLLocalDB
Then I changed my connection string, chaned the datasource to Data Source=MSSQLLocalDB:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=MSSQLLocalDB;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I get the error
Invalid value for key 'attachdbfilename'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Invalid value for key 'attachdbfilename'.
Again I changed my connection string to Data Source= .\SQLEXPRESS
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I got the 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)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: 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 can not figure out what is the problem whether the cause of the problem is connection string or any where else in the code. In other solutions, it is mentioned to edit DBContext Class, but in the tutorial there is no such class created. I am not using MVC, simple web form application trying to implement Identity and OWIN module. Please help me.

The output from your PowerShell script shows that you have a default instance (with the "instance" name of MSSQLSERVER) of SQL Server running:
Running MSSQLSERVER SQL Server (MSSQLSERVER)
This means, you can use Data Source=. (or Data Source=(local)) for your server name (you must not define an instance name to connect to the default instance), define the database name in Initial Catalog, and drop that AttachDbFileName crap once and for all (and let SQL Server handle all the file-related ins and outs - don't mess around with .mdf database files yourself!):
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.;Initial Catalog=WebFormsIdentity;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

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 !

.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"

SQL Profile giving server not found exception

I'm trying to use Profiles in my program. Creating the necessary tables using aspnet_regsql.exe from the command line worked fine. However, A System.Web.HttpException exception is thrown saying it is unable to connect to the database when I try to run the application. This is strange because I am connected to the database on the Server Explorer in VS 2013. Here is the error message on my application's website.
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)
Here's the relevant code in my web config file.
<profile defaultProvider="SqlProvider" inherits="[Namespace].AccountProfile">
<providers>
<clear/>
<add name="SqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="[stringname]"/>
</providers>
<properties>
<add name="Rows" type="System.String"/>
<add name="Area" type="System.String"/>
</properties>
</profile>
And here is the code in my application where the exception is thrown
AccountProfile.cs
public class AccountProfile : ProfileBase
{
static public AccountProfile CurrentUser
{
get { return (AccountProfile)(ProfileBase.Create(Membership.GetUser().UserName)); }
}
....
}
Turns out the Membership.GetUser() part of the code was still trying to use the LocalSqlServer connection string found in the machine.config file instead of the database connection string I provided in the web.config. Adding <clear /> in my connectionString configuration helped me figure this out.
<connectionStrings>
<clear />
<add name="..." connectionString="Data Source=...;Initial Catalog=...;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Can LocalDb and SQL Enterprise 2012 run side by side?

I've just installed SQL Enterprise 2012 and now LocalDb appears to be inoperable. I can access LocalDb from the command line and see that the instance that I am referencing is indeed running, but when I run a project with Code First migrations enabled that uses the instance (localdb)\v11.0, I get an error stating that the connection string may be incorrect. I didn't change the connection string at all, it is the connection string that is set by default while creating a new project in Visual Studio 2012.
Has anyone had the same issue? Is there a fix for running both side by side?
Here's the exact error that I am getting:
An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
And the exception:
System.Data.ProviderIncompatibleException: An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct. ---> System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.Data.SqlClient.SqlException: 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: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.
I just realized what the issue was, hopefully this helps someone else.
Here is the section of my web.config that was causing errors:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v12.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
The parameter v12.0 value was the issue. It seems that after I installed SQL Server 2012, this generated parameter went from v11.0 to v12.0 causing a version conflict. I guess I could have installed the newest version of SQL Server Express (2014) and not had a problem at all since the version number would indeed be v12.0.

Categories