Amehiny
Amehiny

Reputation: 135

How to access the database from the api controller?

I am doing a mobile app with Angular and MVC. I am doing my app calling some dummy data from the json model in the web api. Check my code below:

 private IEnumerable<JsonModel> events = new JsonModel[]
    {
       new JsonModel {Id = 1, Title = "title1"}
       new JsonModel {Id = 2, Title = "title2"}
      };


        // GET api/<controller>
        public IEnumerable<JsonModel> Get()
        {
            return events;
        }

        // GET api/<controller>/5
        public JsonModel Get(int id)
        {
            return events.FirstOrDefault(e => e.Id == id );
        }

What I want to do now is to connect my project to the real database. How can I do that?

Upvotes: 2

Views: 3806

Answers (2)

DanielV
DanielV

Reputation: 2690

Configuring access to the database file itself can be also the source of your isssue, look here: Configuring Permissions for an Access Database

Hope it helps

Upvotes: 0

oskar132
oskar132

Reputation: 839

As the ZenCoder mentioned, there are lots of ways to access your database, one of them is the repository pattern, here's some info about it:

https://msdn.microsoft.com/en-us/library/ff649690.aspx

This is one of the most famous patterns used nowadays and when working with the asp.net mvc framework is used with the Entity Framework. Here's a nice tutorial which explains how to use it with the ASP.NET Web Api2:

http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-1

any question just let me know.

Upvotes: 2

Related Questions