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?


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:

This is one of the most famous patterns used nowadays and when working with the 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:

any question just let me know.