How do you combine Entity Framework with Web API?

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

Question

I'm researching the new ASP.NET MVC4 Web API framework. I'm running Visual Studio 2011 beta on the Windows 8 consumer preview.

My problem is that none of the official samples for the new Web API framework use any kind of database backend. In the past, I've been able to create a local SQL CE database and serve it up via a WCF Data Service using Entity Framework as the ORM. How do I do the same with Web API?

Also, is this even a valid question? Should I just keep using a WCF Data Service if I want to expose an Entity Framework mapped SQL CE database? It seems to work fine, other than not offering the flexibility to choose response formatters that I might get with web api.

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

Accepted Answer

If you look at the official Contact Manager sample, you'll find that the Repository Pattern is used to access the data layer. Also, bear in mind that in this particular example there's also DI via Ninject.

In my case I've easily plugged this onto an already existing EF model.

Here's an example for a repository implementation

///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);
    }
}

Your mileage may vary though, and you might want to abstract out some of this data access even further. Good news is that plenty of patterns and ideas that you may have already used on MVC-based projects are still valid.

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

Popular Answer

I haven't worked with WCF Web API, so I can say for sure if you can use it same way as you did with WCF Web API, but I'm sure you can use EF with ASP.NET Web API. I suggest you take a look at how ASP.NET MVC makes use of EF, it should be very similar how you would use it with ASP.NET Web API.

As for other question, if you're planning some new development you should consider using ASP.NET Web API since there's an announcement on wcf.codeplex.com saying:

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