Entity Framework doesn't recognize the connection - c#

I got a demo C# solution from a third-party written using VS2019. They use the Northwind database and entity framework. It's an MVC 4 project. They have the connection string as follows:
<add name="NorthwindEntities" connectionString="metadata=res://*/Models.Northwind.csdl|res://*/Models.Northwind.ssdl|res://*/Models.Northwind.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Northwind.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I have replaced the above connection string with this one.
<add name="NorthwindEntities" connectionString="metadata=res://*/Models.Northwind.csdl|res://*/Models.Northwind.ssdl|res://*/Models.Northwind.msl;provider=System.Data.SqlClient;provider connection string="data source=MYSERVER_NAME\v15.0;attachdbfilename=|DataDirectory|\Northwind.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
When I ran the solution, I got the following 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 am new to asp.net MVC and entity framework. So, I am not sure what "metadata=res:///Models.Northwind.csdl|res:///Models.Northwind.ssdl|res://*/Models.Northwind.msl;" means or why it's used. In a web forms applications, I use the following connection string, which works fine.
"Server=MYSERVER_NAME;Database=NORTHWIND;Trusted_Connection=True;Encrypt=false"
public ActionResult
Products_Read([DataSourceRequest]DataSourceRequest request)
{
using (var northwind = new NorthwindEntities())
{
IQueryable<Product> products = northwind.Products;
// Convert the Product entities to ProductViewModel
//instances
DataSourceResult result =
products.ToDataSourceResult(request, product => new
ProductViewModel
{
ProductID = product.ProductID,
ProductName = product.ProductName,
UnitsInStock = product.UnitsInStock
});
return Json(result);
}
}
This line of the above code, "products.ToDataSourceResult(request,
product => new ProductViewModel" throws the error
Their database is SQL server express and mine is a SQL server developer edition. I wonder how I could update the connection string from the third-party code.

Finally, I have resolved the issue. So, the only change was “(LocalDB)\v11.0” to “(LocalDB)\MSSQLLocalDB”. This may be because v11.0 works only in SQL server 2014. Since I had SQL server 2016 and later, I had to change it to the one shown above. Link: How to install localdb separately?

Related

C# Update Connection String Visual Studio

I am having some connectivity issues with Visual Studio and SQL Server.
This is the connections string in my web.config file;
<connectionStrings>
<add name="WintipToysConnectionString" connectionString="Data
Source=localhost\sqlexpress;Initial Catalog=WINGTIPTOYS;Integrated
Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
According to my DB connection string found in Visual Studio, my connection string is;
Data Source=localhost\sqlexpress;Initial Catalog=WINGTIPTOYS;Integrated Security=True
Which seems to add up - but, I get this error when my program hits a certain line;
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. Cannot create an automatic instance. See the Windows Application event log for error details.
I am actually following the WingTipToys tutorial on the Microsoft website and I am struggling to get it to use the Database I attached to SSMS.
I can write queries inside VS which works fine. So I don't really understand why my program is having this error when running?
Thanks.
Admittedly haven't used SQLExpress in a looong time. It looks like the syntax you're using is for an "instance" - which is pointed out in the error message:
...Cannot create an automatic instance...
What you have does look like server\instance conn string syntax...
Connect to the "default" instance of SQL Express like so: Data Source=.\\SQLExpress
Refer to this:
SSE Connection string sample
Hth...

Website cannot establish connection to the sql on server

The website runs perfectly fine on my local computer with connection string containing database information of already hosted server. but when i upload the website on the server i get this error. the main page of the website is loading but the other features that has sql connection accessibility are not.
System.ComponentModel.Win32Exception: Access is denied
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: 56 - Unable to load the SQLUserInstance.dll
from the location specified in the registry. Verify that the Local
Database Runtime feature of SQL Server Express is properly
installed.)]
My local default connection string is
<add name="defaultconnection" connectionString="Data Source=(LocalDb)\mssqllocaldb;Initial Catalog=aspnet-fuzzzz-20160406102902;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-fuzzzz-20160406102902.mdf" providerName="System.Data.SqlClient" />
Apart from that i have the entity framework model connection string that contains sql authentication of a remote server that contains data..
Revised and additional information -
my filter looks like this could it be the server is by default taking my default connection string instead of my entity framework connection string
public SimpleMembershipInitializer()
{
Database.SetInitializer<UsersContext>(null);
try
{
using (var context = new UsersContext())
{
if (!context.Database.Exists())
{
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
}

How to connect to Oracle DB from .NET?

When I open SQL Command Line, I write CONNECT username/password#[//]host[:port][/service_name] and it connects me to the database just fine. However, I'm unable to connect from a .NET project using a connection string. I have tried many things such as <add name="ConnectionString" connectionString="Data Source=username/password#[//]host[:port][/service_name];" /> and <add name="ConnectionString" connectionString="server=tcp:host;Initial Catalog=service_name; user id=username; password=password; Connection Timeout=180;" providerName="System.Data.OracleClient" />, but nothing worked so far. Whenever I get to sconn.Open(); in the following:
var CurrentConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sconn = new SqlConnection(CurrentConnectionString);
sconn.Open();
I practically always get the following 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: 25 - Connection string is not valid)
How can I connect to the database properly?
Try following connection string
string con = "Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST = 000.00.0.00)(PORT = 0000)))(CONNECT_DATA =(SERVICE_NAME = database)));User ID=User/Schema;Password=password;Unicode=True";
& then use this conection string as follow
using (OracleConnection objConn = new OracleConnection(con))
{
\\ code
}
I highly recommend using the "Official Oracle ODP.NET, Managed Driver".
https://www.nuget.org/packages/Oracle.ManagedDataAccess/
Or the one for Entity framework:
https://www.nuget.org/packages/Oracle.ManagedDataAccess.EntityFramework/
After installing via Visual Studio it opens a readme that I also recommend you read.
Oracle provide massive amounts of documentation. This is where to start:
http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.html
You use System.Data.SqlClient.SqlConnection which is used to connect to a (Microsoft) SQL Server, it cannot be used for an Oracle connection.
You should use System.Data.OracleClient.OracleConnection or Oracle.ManagedDataAccess.Client.OracleConnection
Have a look at this answer to see other possibilities:
How to connect to Oracle 11 database from . net
You can always use EF, and use create one ADO.Net Entity Data Model
And use the wizard to create and test the connection

Cannot Connect to Server - (with adding a new class and connecting to DB)

So I can actually connect to the server as it is localhosted. But when I created a class called Merchant and add the /Merchants to my local host address I get the above error, even though the server does just fine with all the other pages from the web site.
This is my first time messing with C# and Visual Studios, and I am following a tutorial but I got stuck here.
Using MS Visuals 2013, MCV 5.
Here is the Merchants function:
// GET: Merchants
public ActionResult Index()
{
return View(db.Merchants.ToList());
}
Here is the connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source= (LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20150610102919.mdf;Initial Catalog=aspnet-WebApplication1-20150610102919;Integrated Security=True"
providerName="System.Data.SqlClient" />
Here is the full error message:
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 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.)

MVC4 website only works on one PC, not other PC, fails with "SQL Network Interfaces, error: 26"

The following MVC4 ef-codefirst application works fine on my computer, but nowhere else.
VS 2012 Ultimate and SQL Express is installed on both computers.
Both computers use this connection string:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-AppointmentMVC4-20130118154339;
Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-AppointmentMVC4-20130118154339.mdf"
providerName="System.Data.SqlClient" />
When trying to access the database from computers other than the development computer:
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)
The working machine has this DB connection:
Use of localDB or sqlexpress as connection does not matter; getting this to succeed matters.
DbContext class:
public class AppointmentContext : DbContext
{
public DbSet<Appointment> Appointments { get; set; }
public DbSet<User> Users { get; set; }
}
I think that LocalDB only supports local connections via named pipes protocol.
http://www.sqlcoffee.com/SQLServer2012_0004.htm
Somehow this fixed it all.
<add name="AppointmentContext" connectionString="Data Source=
(LocalDb)\v11.0;AttachDBFilename=|DataDirectory|\Appointments.mdf"
providerName="System.Data.SqlClient" />
Thank you Iraklis and jessehouwing for your help, great community.

Categories