Setting up web api to pull data from a database using EF.http://forums.asp.net/t/1792638.aspx/1?Setting+up+web+api+to+pull+data+from+a+database+using+EF+Sun, 22 Apr 2012 17:18:56 -040017926384931442http://forums.asp.net/p/1792638/4931442.aspx/1?Setting+up+web+api+to+pull+data+from+a+database+using+EF+Setting up web api to pull data from a database using EF. <p>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.&nbsp; I know how it gets pulled from a controller.</p> <p>Controller</p> <pre class="prettyprint">var categorypatents = db.CategoryPatents.Include(c =&gt; c.Category).Include(c =&gt; c.Patent); return View(categorypatents.ToList());</pre> <p>So inside of my web api how would I accomplish this?&nbsp; I know how to set the varibles if I'm defining them in my controller.<br> <br> </p> 2012-04-13T15:16:30-04:004931656http://forums.asp.net/p/1792638/4931656.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p>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.&nbsp; I just can't figure out how to do what within the ApiContoller.</p> 2012-04-13T17:58:04-04:004931704http://forums.asp.net/p/1792638/4931704.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p>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.&nbsp; I'm not sure how to pull this within a Api Controller.</p> <p>So instead of say 1 showing up, I need to to pull the name from the other model class and says &quot;football.&quot;</p> 2012-04-13T18:45:21-04:004931937http://forums.asp.net/p/1792638/4931937.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p>I am not sure if you are familar with dependency injection using a ioc container and I hope I can point you</p> <p>on the right direction</p> <p>This is what you need to do</p> <p>1 BUILd a repository layer with your sql statements <a href="http://www.tugberkugurlu.com/archive/asynchronous-database-calls-with-task-based-asynchronous-programming-model-tap-in-asp-net-mvc-4"> http://www.tugberkugurlu.com/archive/asynchronous-database-calls-with-task-based-asynchronous-programming-model-tap-in-asp-net-mvc-4</a>&nbsp;example</p> <p>one of the problems you will have to face in thislayer is lifetime management session management, &nbsp;&nbsp;http message handler&nbsp;you can vote on this</p> <p>2 build a service layer(keep this light weight)</p> <p>3 build your controller(controllers use webservice similar to rest (service contracts) post get etc THIs is similar to java spring framework</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p>&nbsp;</p> 2012-04-14T02:02:44-04:004934921http://forums.asp.net/p/1792638/4934921.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p>So there is no way to use what's done in a controller model to get the category name / patent number.</p> <p>Below is the Index for my CategoryPatent model:</p> <pre class="prettyprint">// // GET: /CategoryPatent/ public ActionResult Index() { var categorypatents = db.CategoryPatents.Include(c =&gt; c.Category).Include(c =&gt; c.Patent); return View(categorypatents.ToList()); }</pre> <p>Which I found out i can't do the same within my APIController to pull the category name / patent number from their respective models.</p> <p>&nbsp;</p> <p>&nbsp;</p> 2012-04-16T14:23:49-04:004935030http://forums.asp.net/p/1792638/4935030.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p>I am not sure if this will help.....</p> <p>DId you set up your custom controller class.....<a href="http://netmvc.blogspot.com/2012/03/aspnet-mvc-4-webapi-support-areas-in.html">http://netmvc.blogspot.com/2012/03/aspnet-mvc-4-webapi-support-areas-in.html</a></p> <p>&nbsp;you need to use dependency injection, because the controller classes do not automatically read data.....&nbsp;&nbsp;</p> 2012-04-16T15:29:23-04:004939501http://forums.asp.net/p/1792638/4939501.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p>Well here are my 3 models:</p> <p>Patent</p> <pre class="prettyprint">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&lt;CategoryPatent&gt; CategoryPatent { get; set; } } }</pre> <p>Category</p> <pre class="prettyprint">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&lt;CategoryPatent&gt; CategoryPatent { get; set; } } }</pre> <p>CategoryPatent</p> <pre class="prettyprint">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; } } }</pre> <p>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.&nbsp; Everything I've tried doesn't seem to work.<br> <br> <br> </p> <p>&nbsp;</p> 2012-04-18T18:05:09-04:004939642http://forums.asp.net/p/1792638/4939642.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p><a href="http://forums.asp.net/p/1793998/4937595.aspx/1?Cannot&#43;use&#43;Entities&#43;in&#43;Entity&#43;Framework&#43;directly&#43;with&#43;Web&#43;API">http://forums.asp.net/p/1793998/4937595.aspx/1?Cannot&#43;use&#43;Entities&#43;in&#43;Entity&#43;Framework&#43;directly&#43;with&#43;Web&#43;API</a>&#43;</p> <p><a href="http://forums.asp.net/t/1773164.aspx/1">http://forums.asp.net/t/1773164.aspx/1</a></p> <p>apparently this is a bug in the asp.net mvc web api &nbsp;</p> 2012-04-18T20:06:01-04:004939665http://forums.asp.net/p/1792638/4939665.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p>If you still are interested you will have to create your own json serializer to read data</p> <p><a href="http://www.bluelemoncode.com/post/2012/03/04/CRUD-operation-using-Web-API-in-Aspnet-Web-form-application.aspx">http://www.bluelemoncode.com/post/2012/03/04/CRUD-operation-using-Web-API-in-Aspnet-Web-form-application.aspx</a></p> <p><a href="http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx">http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx</a></p> <p>&nbsp;&nbsp;&nbsp;</p> <p>&nbsp;</p> 2012-04-18T20:33:58-04:004939671http://forums.asp.net/p/1792638/4939671.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p><a href="http://www.west-wind.com/weblog/posts/2012/Mar/09/Using-an-alternate-JSON-Serializer-in-ASPNET-Web-API">http://www.west-wind.com/weblog/posts/2012/Mar/09/Using-an-alternate-JSON-Serializer-in-ASPNET-Web-API</a></p> 2012-04-18T20:38:40-04:004943393http://forums.asp.net/p/1792638/4943393.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p>I've already setup all the JSON readers.&nbsp; The issue is just that I can't pull say the Category Name / Patent Number&nbsp;from reading throug the categorypatent table.</p> 2012-04-20T17:49:37-04:004943474http://forums.asp.net/p/1792638/4943474.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p>So the real question is that since my CategoryPatent model is setup with 2 virtuals to my Category / Patent models is it possible to pull the specific field elements by just using the CategoryPatent model or do I need to do more work to pass back those values?</p> 2012-04-20T19:21:04-04:004944107http://forums.asp.net/p/1792638/4944107.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>MasterV23</h4> <span class="pln">&nbsp; &nbsp;</span><span class="kwd">return</span><span class="pln"> </span> <span class="typ">View</span><span class="pun">(</span><span class="pln">categorypatents</span><span class="pun">.</span><span class="typ">ToList</span><span class="pun">());</span><span class="pln"></span></blockquote> <p></p> <p>There is no View method in Web API. You need learn more about Web API. See, this <a href="http://lostechies.com/jimmybogard/2012/04/10/asp-net-web-api-mvc-viewmodels-and-formatters/"> article</a>.</p> 2012-04-21T13:54:34-04:004944426http://forums.asp.net/p/1792638/4944426.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. Yes I know there are no views in web API, that was just to show what was in my controller. I'm just wonder if there is a way or not to access model information from another model with web api's like how it's d one within a controller with the .include(). 2012-04-22T02:35:00-04:004944431http://forums.asp.net/p/1792638/4944431.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>MasterV23</h4> Yes I know there are no views in web API, that was just to show what was in my controller. I'm just wonder if there is a way or not to access model information from another model with web api's like how it's d one within a controller with the .include(). </blockquote> <p></p> <p>The DbContext class is same in Web APi and MVC. </p> <p></p> 2012-04-22T02:54:37-04:004944841http://forums.asp.net/p/1792638/4944841.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. Then why is it not possible to use it in the same contest that I'm using in my controller to get the name and number? --&gt; db.CategoryPatents.Include(c =&gt; c.Category).Include(c =&gt; c.Patent); 2012-04-22T13:58:55-04:004945003http://forums.asp.net/p/1792638/4945003.aspx/1?Re+Setting+up+web+api+to+pull+data+from+a+database+using+EF+Re: Setting up web api to pull data from a database using EF. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>MasterV23</h4> Then why is it not possible to use it in the same contest that I'm using in my controller to get the name and number? --&gt; db.CategoryPatents.Include(c =&gt; c.Category).Include(c =&gt; c.Patent); </blockquote> <p></p> <p>Can you provide a sample application?</p> <p></p> 2012-04-22T17:18:56-04:00