Luis D Urraca
Luis D Urraca

Reputation: 2084

Connecting to an existing database in a ASP.NET MVC4 Web API project

I'm building an web api using ASP.NET web api framework. I have seen a few tutorials on how to get started but I haven't found to use an existing SQL Server database. I have the database up and running also added the data connection to the database explorer in VS but I don't know how to connect the database to my project so I can start using it as my repository. How can I do that?

Upvotes: 1

Views: 2702

Answers (1)

Dmitry S.
Dmitry S.

Reputation: 8513

Put the following in your application web.config file and replace "yourConnectionString" with the actual connection string.

<configuration>
       <connectionStrings>
           <add name="DbConnection" connectionString="yourConnectionString" />
       </connectionStrings>
</configuration>

In the code use System.Configuration.ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString to get its value. You might need to reference the "System.Configuration" assembly in your project.

Upvotes: 1

Related Questions