How do you combine Entity Framework with Web API?

asp.net asp.net-mvc asp.net-web-api entity-framework

Question

I'm learning more about the brand-new ASP.NET MVC4 Web API framework. I'm using the Windows 8 Consumer Preview and the Visual Studio 2011 beta.

My issue is that there isn't a single database backend used in any of the official examples for the new Web API framework. Using Entity Framework as the ORM in the past, I was able to build a local SQL CE database and deliver it over a WCF Data Service. How can I do the same using Web API?

Is this really a question at all? If I want to expose a SQL CE database translated to Entity Framework, should I simply keep using a WCF Data Service? With the exception of not giving me the option to choose answer formatters that I may obtain with a web api, everything appears to operate ok.

1
8
3/6/2012 12:10:00 AM

Accepted Answer

You may access the data layer by using the Repository Pattern, according to the official Sample of a contact manager. Keep in mind that DI through Ninject is also included in this specific scenario.

In my situation, I was able to quickly insert this into an existing EF model.

Here is an example of how to implement a repository.

///MODEL
public class SampleRepository : ISampleRepository
{

    public IQueryable<Users> GetAll()
    {
        SampleContext db = new SampleContext();
        return db.users;
    }

    [...]
}

///CONTROLLER
private readonly ISampleRepository repository;

public SampleController(ISampleRepository repository)
{
    this.repository = repository;
}

//GET /data
public class SampleController : ApiController
{
    public IEnumerable<DataDTO> Get()
    {
        var result = repository.GetAll();

        if (result.Count > 0)
        {
            return result;
        }


        var response = new HttpResponseMessage(HttpStatusCode.NotFound);
        response.Content = new StringContent("Unable to find any result to match your query");
        throw new HttpResponseException(response);
    }
}

However, your results may differ, and you may wish to further abstract some of this data access. The good news is that there are several patterns and concepts that you may have previously used to MVC-based applications.

4
3/6/2012 12:05:16 PM

Popular Answer

Although I haven't used WCF Web API, I can tell with certainty that you can use EF with ASP.NET Web API in the same manner as you did with WCF Web API. I advise you to look into ASP.NET MVC utilizes EF. since using it with the ASP.NET Web API should be pretty similar.

Regarding the other query, there is an announcement on wcf.codeplex.com indicating that if you're planning any new work you should consider utilizing ASP.NET Web API.

WCF Web API is now ASP.NET Web API! ASP.NET Web API released with ASP.NET MVC 4 Beta. The WCF Web API and WCF Support for jQuery content on this site will be removed by the end of 2012.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow