Exception while deploying ASP.NET Website after Using EF - entity-framework

I have two projects, one containing WS which access to the DB and the other one is front-end.
The two projects are deployed on seperate servers.
Before Using EF and linq for accessing the DB in the WS project, everything was working fine, but after that I got and exception saying "the underlying provider failed on open".
Please ca you help me with that. Thank you in advance.
Here is my ConnectionString (WS Project):
<connectionStrings>
-->
<add name="ConString1" connectionString="Data Source=#address,1433;Initial Catalog = MpCarteCoBrandee;MultipleActiveResultSets=True;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
<!--<add name="ConString" connectionString="Data Source=AOUS-PC;Initial Catalog=TestMobilePayment;Integrated Security=SSPI" providerName="System.Data.SqlClient" />-->
<add name="MobilePaymentEntities" connectionString="metadata=res://*/MobilePayment.csdl|res://*/MobilePayment.ssdl|res://*/MobilePayment.msl;provider=System.Data.SqlClient;provider connection string="data source=#address,1433;initial catalog=MobilePayment;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

Related

EntityFramework connect to Visual Studio SQL Server (localdb)

I am running a local instance of Sql Server through Visual Studio:
It is online and I am able to view the data in the database through the UI.
My App.config file looks like the following:
<?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="EntitiesContext" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" providerName="System.Data.SqlClient"/>
<add name="DefaultConnection" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" 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>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup>
</configuration>
And my EntitiesContext looks like the following:
Could someone please help with what I am missing as this keeps failing connection with the following:
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)'
Inner Exception
Win32Exception: The system cannot find the file specified
The issue is that the connection string you are using is reverting to a named pipe connection, and it's telling you that nothing is listening on that Pipe.
Changing it to "localhost\MSSQLLocalDB;...." should work if your local database is set up for Shared Memory connections.
To check what protocols are enabled on your server you can run MMC.exe then add the plugin for SQL Server Configuration Manager. Under SQL Server Network Configuration it will show whether your SQL Server instance is allowing Named Pipes or TCP/IP. I believe by default SQL Express only enables Shared Memory which is fine for local testing purposes.

MVC Connection Strings/LocalDb

Fairly new to MVC so this probably seems like a fairly newbie question.
I've been developing my application using a SQLExpress instance/LocalDb, I'm now ready to deploy my site to production, so my question is how do I configure my web.config file for this environment?
My database has been created in production, I'm just struggling with establishing the connection strings.
I'm using a host that I think does not support LocalDB (GearHost), but sources seem to suggest this is possible (even though I've not found any decent examples).
My current development connection strings are below.
Thanks
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MyApp-20160204160648;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MyApp-20160204160648.mdf" />
<add name="MyEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=MyPC\SQLEXPRESS;initial catalog=MyDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
I have a MVC project.I use two conectionString as following
<add name="matematikEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;provider connection string="server=MYSERVERNAMEONHOST;user id=MYUSERID;password=MYPASSWORD;persistsecurityinfo=True;database=MYDBNAME"" providerName="System.Data.EntityClient" />
<add name="matematikEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=MYPASSWORD;persistsecurityinfo=True;database=MYDBNAME"" providerName="System.Data.EntityClient" />
When i publish the project,i comment first one and second is active,when i work on my PC i comment second and first one is active.This works.

Web Deploy removing quotes in connection string

The last quote escape is being removed in the webserver web.config when using webdeploy.
Any ideas why?
I've currently having to log on to the server and readd it everytime I publish the site!
<add name="Entities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string="server=server;database=db;user id=1;password=password;MultipleActiveResultSets=True;App=EntityFramework;"" providerName="System.Data.EntityClient" />
becomes:
<add name="Entities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string="server=server;database=db;user id=1;password=password;MultipleActiveResultSets=True;App=EntityFramework;" providerName="System.Data.EntityClient" />
Try to remove ";" (semicolon) before
&quot
try this,
<add name="Entities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string="server=server;database=db;user id=1;password=password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

EF6 - database name in config

I'm looking to set the database name in EF6 through the app.config file (and not using the constructor of the data context). I have the provider and database initializer in the config, but where does the database name go? What's the configuration line look like?
Cheers,
The catalog name in connection string
<connectionStrings>
<add name="MyContextName" connectionString="Data Source=localhost;Initial Catalog=TheDbName;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>

EF4 The provider did not return a Provider Manifest Token string

Yes, one more question about Provider manifest token. Unfortunately all previous 22 questions was not useful to solve my problem. I developing simple web application using MVC4 + Code First + Sql Express.
Here is my context descendant:
public class MCQContext : DbContext
{
public MCQContext()
: base("name=ApplicationConnection")
{
}
...............
}
And here - part of web.config related to the problem:
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</configSections>
<connectionStrings>
<add name="ApplicationConnection"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="name=ApplicationConnection" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
So, as you can see, correct connection string passed to base of context class (I got the same error if I rename connection string to "MCQContext" and do not pass anything to parent context class).
I have no idea how to fix it. This behavior reproduced if I creating absolutely empty MVC4 application, remove all packages (I prefer manually specify required assemblies and do not use NuGet) and fix references (including reference to sqlserver express).
The problem with your connection string here is:
<add name="TrempimModel"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|aspnetdb.sdf;
User Instance=true"
providerName="System.Data.SqlClient" />
You're basically defining what "server" you're connecting to - but you're not saying what database inside the file to connect to. Also - the file extension for SQL Server Express database files is .mdf (not .sdf - that's SQL Server Compact Edition) - you need to take that into account, too! (was a typo, according to comment by OP).
You need to define an extra database=.... (or Initial Catalog=.....) in your connection string:
<add name="TrempimModel"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
database=YourDatabaseName;
AttachDBFilename=|DataDirectory|aspnetdb.mdf;
User Instance=true"
providerName="System.Data.SqlClient" />
Then it should work just fine.