I've seen examples where you can setup the value of the model but my question is I have a DB with data I want to pass to my web api. I know how it gets pulled from a controller.
Controller
var categorypatents = db.CategoryPatents.Include(c => c.Category).Include(c => c.Patent);
return View(categorypatents.ToList());
So inside of my web api how would I accomplish this? I know how to set the varibles if I'm defining them in my controller.
So what I need to do in my API contoller is be able to read my record(s) from my db and send them to the user. I just can't figure out how to do what within the ApiContoller.
Ok looks like I solved one issue where I'm able to send all the information from my Model through the API but the issue now is that the Model references another Model class which has the descriptions of that item. I'm not sure how to pull this within a
Api Controller.
So instead of say 1 showing up, I need to to pull the name from the other model class and says "football."
namespace Test.Models
{
public class Patent
{
public int PatentId { get; set; }
[Required]
public string Number { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
[Required]
public bool IsActive { get; set; }
[Required]
public int CountryId { get; set; }
public virtual Country Country { get; set; }
public ICollection<CategoryPatent> CategoryPatent { get; set; }
}
}
Category
namespace Test.Models
{
public class Category
{
public int CategoryId { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
[Required]
public bool IsActive { get; set; }
public virtual ICollection<CategoryPatent> CategoryPatent { get; set; }
}
}
CategoryPatent
namespace Test.Models
{
public class CategoryPatent
{
public int CategoryPatentId { get; set; }
[Required]
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
[Required]
public int PatentId { get; set; }
public virtual Patent Patent { get; set; }
[Required]
public bool IsActive { get; set; }
}
}
So within my API I'm pulling information from the CategoryPatent model but I need the Category Name from the Category table and Patent Number from the Patent table. Everything I've tried doesn't seem to work.
MasterV23
Member
113 Points
318 Posts
Setting up web api to pull data from a database using EF.
Apr 13, 2012 03:16 PM|LINK
I've seen examples where you can setup the value of the model but my question is I have a DB with data I want to pass to my web api. I know how it gets pulled from a controller.
Controller
So inside of my web api how would I accomplish this? I know how to set the varibles if I'm defining them in my controller.
MasterV23
Member
113 Points
318 Posts
Re: Setting up web api to pull data from a database using EF.
Apr 13, 2012 05:58 PM|LINK
So what I need to do in my API contoller is be able to read my record(s) from my db and send them to the user. I just can't figure out how to do what within the ApiContoller.
MasterV23
Member
113 Points
318 Posts
Re: Setting up web api to pull data from a database using EF.
Apr 13, 2012 06:45 PM|LINK
Ok looks like I solved one issue where I'm able to send all the information from my Model through the API but the issue now is that the Model references another Model class which has the descriptions of that item. I'm not sure how to pull this within a Api Controller.
So instead of say 1 showing up, I need to to pull the name from the other model class and says "football."
billsm
Member
462 Points
220 Posts
Re: Setting up web api to pull data from a database using EF.
Apr 14, 2012 02:02 AM|LINK
I am not sure if you are familar with dependency injection using a ioc container and I hope I can point you
on the right direction
This is what you need to do
1 BUILd a repository layer with your sql statements http://www.tugberkugurlu.com/archive/asynchronous-database-calls-with-task-based-asynchronous-programming-model-tap-in-asp-net-mvc-4 example
one of the problems you will have to face in thislayer is lifetime management session management, http message handler you can vote on this
2 build a service layer(keep this light weight)
3 build your controller(controllers use webservice similar to rest (service contracts) post get etc THIs is similar to java spring framework
MasterV23
Member
113 Points
318 Posts
Re: Setting up web api to pull data from a database using EF.
Apr 16, 2012 02:23 PM|LINK
So there is no way to use what's done in a controller model to get the category name / patent number.
Below is the Index for my CategoryPatent model:
// // GET: /CategoryPatent/ public ActionResult Index() { var categorypatents = db.CategoryPatents.Include(c => c.Category).Include(c => c.Patent); return View(categorypatents.ToList()); }Which I found out i can't do the same within my APIController to pull the category name / patent number from their respective models.
billsm
Member
462 Points
220 Posts
Re: Setting up web api to pull data from a database using EF.
Apr 16, 2012 03:29 PM|LINK
I am not sure if this will help.....
DId you set up your custom controller class.....http://netmvc.blogspot.com/2012/03/aspnet-mvc-4-webapi-support-areas-in.html
you need to use dependency injection, because the controller classes do not automatically read data.....
MasterV23
Member
113 Points
318 Posts
Re: Setting up web api to pull data from a database using EF.
Apr 18, 2012 06:05 PM|LINK
Well here are my 3 models:
Patent
namespace Test.Models { public class Patent { public int PatentId { get; set; } [Required] public string Number { get; set; } [Required] public string Name { get; set; } public string Description { get; set; } [Required] public bool IsActive { get; set; } [Required] public int CountryId { get; set; } public virtual Country Country { get; set; } public ICollection<CategoryPatent> CategoryPatent { get; set; } } }Category
namespace Test.Models { public class Category { public int CategoryId { get; set; } [Required] public string Name { get; set; } public string Description { get; set; } [Required] public bool IsActive { get; set; } public virtual ICollection<CategoryPatent> CategoryPatent { get; set; } } }CategoryPatent
namespace Test.Models { public class CategoryPatent { public int CategoryPatentId { get; set; } [Required] public int CategoryId { get; set; } public virtual Category Category { get; set; } [Required] public int PatentId { get; set; } public virtual Patent Patent { get; set; } [Required] public bool IsActive { get; set; } } }So within my API I'm pulling information from the CategoryPatent model but I need the Category Name from the Category table and Patent Number from the Patent table. Everything I've tried doesn't seem to work.
billsm
Member
462 Points
220 Posts
Re: Setting up web api to pull data from a database using EF.
Apr 18, 2012 08:06 PM|LINK
http://forums.asp.net/p/1793998/4937595.aspx/1?Cannot+use+Entities+in+Entity+Framework+directly+with+Web+API+
http://forums.asp.net/t/1773164.aspx/1
apparently this is a bug in the asp.net mvc web api
billsm
Member
462 Points
220 Posts
Re: Setting up web api to pull data from a database using EF.
Apr 18, 2012 08:33 PM|LINK
If you still are interested you will have to create your own json serializer to read data
http://www.bluelemoncode.com/post/2012/03/04/CRUD-operation-using-Web-API-in-Aspnet-Web-form-application.aspx
http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx
billsm
Member
462 Points
220 Posts
Re: Setting up web api to pull data from a database using EF.
Apr 18, 2012 08:38 PM|LINK
http://www.west-wind.com/weblog/posts/2012/Mar/09/Using-an-alternate-JSON-Serializer-in-ASPNET-Web-API