I need to create a service (currently with ASP.NET Web Api) that is taking data from SQL database and exposes them using OData protocol. I'm extremely new to OData and Web Api. The problem is i don't know how to perform crud operations - i've looked through some samples available online but my case is a little bit more complicated (or maybe not, hard to say). I'm using entity framework. Sometimes i need to do the "GET" with quering table directly but do "POST" with stored procedure. The biggest problem i have is limitation in parameters amount. As i understand i can have only one. This is rather an issue in my case:
public class ProjectsController : ODataController
{
[EnableQuery]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public IQueryable<GetProjects_Result> Get(string key)
{
var context = new OPMLVSQL001Entities();
IQueryable<GetProjects_Result> results = context.GetProjects(key);
return results;
}
[EnableQuery]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public GetProject_Result Get(int projectId, string loginName)
{
var context = new OPMLVSQL001Entities();
GetProject_Result result = context.GetProject(projectId, loginName).FirstOrDefault();
return result;
}
that code won't work - just to get all projects, i need to pass a login parameter. to get one entity i need to use different method - and i have to pass two parameters. which i don't know how to do in odata (i thought of a hack - passing json table in the only parameter i can use).
So is there a way to use more than one parameter?
i also have to implement a "create" method - an entity is being created with stored procedure again. What is the best way to follow in this case?
one way probably is to add, Project entity to the project and do post like this:
[EnableQuery]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public HttpResponseMessage Post(Project project)
{
}
but what if i have some logic in dbo.CreateProject([bunch of parameters]) that i need to run along and for that reason i'd like to run the stored proc. use values from projects to initialize dbo.CreateProjects parameters? I really don't know what's the best way to follow.
Aucun commentaire:
Enregistrer un commentaire