Skip to content Skip to sidebar Skip to footer

Connecting To Sql Server From Restful C# Service

I am trying to develop a restful C# service with a connection to Microsoft SQL database. From what I understand, I am supposed to do the connection to the SQL server inside the fol

Solution 1:

This may be a helpful start: https://msdn.microsoft.com/en-us/library/jj729737%28v=vs.113%29.aspx?f=255&MSPPError=-2147217396 but probably the step you are missing is having the connection string in your Web.Config.

ASP.Net Entity Framework will try to connect to a database instance whose connection string name in Web.Config matches the name of your context (e.g. WebserverContext, in your example above).

In your Web.Config, your ConnectionStrings element will have something along the following lines:

<connectionStrings><addname="WebserverContext"connectionString="Data Source=localhost;Initial Catalog=myWebserverContextDb;User ID=someuser;Password=somepassword"providerName="System.Data.SqlClient" /></connectionStrings>

Your DbContext class constructor needs to have that same name set so that it can look the connection string up. This tutorial might also help: https://www.tutorialspoint.com/entity_framework/entity_framework_dbcontext.htm

Post a Comment for "Connecting To Sql Server From Restful C# Service"