priti srivastava

priti srivastava

  • NA
  • 81
  • 4.2k

how to display data from self nested table using web api in

Oct 24 2016 7:23 AM
i have a table which is licked to itself . and also one another table. followings are my code lines.when i run this api in browser i got error of serialization.  Please help me. I am new learner of MVC and Web Api
 
 
Model.cs
 
namespace firstMvc1.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("CATEGORIES")]
public partial class CATEGORy
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public CATEGORy()
{
ARTICELS = new HashSet<ARTICEL>();
CATEGORIES1 = new HashSet<CATEGORy>();
}
public int ID { get; set; }
[StringLength(200)]
public string CATEGORY_NAME { get; set; }
public int? PARENT_ID { get; set; }
public bool? STATUS { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ARTICEL> ARTICELS { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<CATEGORy> CATEGORIES1 { get; set; }
public virtual CATEGORy CATEGORy1 { get; set; }
}
}
dataModel.cs
 
namespace firstMvc1.Models
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
public partial class ModelData : DbContext
{
public ModelData()
: base("name=ModelData")
{
}
public virtual DbSet<ARTICEL> ARTICELS { get; set; }
public virtual DbSet<CATEGORy> CATEGORIES { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ARTICEL>()
.Property(e => e.TITLE)
.IsUnicode(false);
modelBuilder.Entity<ARTICEL>()
.Property(e => e.CONTENT)
.IsUnicode(false);
modelBuilder.Entity<ARTICEL>()
.Property(e => e.ArticleImage);
modelBuilder.Entity<CATEGORy>()
.Property(e => e.CATEGORY_NAME)
.IsUnicode(false);
modelBuilder.Entity<CATEGORy>()
.HasMany(e => e.CATEGORIES1)
.WithOptional(e => e.CATEGORy1)
.HasForeignKey(e => e.PARENT_ID);
}
}
}
 ApiController.cs
 
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using firstMvc1.Models;
namespace firstMvc1.Controllers
{
public class CATEGORyApiController : ApiController
{
private ModelData db = new ModelData();
// GET: api/CATEGORyApi
public IQueryable<CATEGORy> GetCATEGORIES()
{
return db.CATEGORIES;
//var cat = db.CATEGORIES.Include("ARTICELS").Select(a=>new {a.CATEGORY_NAME,a.ID });
//return cat;
}
// GET: api/CATEGORyApi/5
[ResponseType(typeof(CATEGORy))]
public IHttpActionResult GetCATEGORy(int id)
{
CATEGORy cATEGORy = db.CATEGORIES.Find(id);
if (cATEGORy == null)
{
return NotFound();
}
return Ok(cATEGORy);
}
// PUT: api/CATEGORyApi/5
[ResponseType(typeof(void))]
public IHttpActionResult PutCATEGORy(int id, CATEGORy cATEGORy)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != cATEGORy.ID)
{
return BadRequest();
}
db.Entry(cATEGORy).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!CATEGORyExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/CATEGORyApi
[ResponseType(typeof(CATEGORy))]
public IHttpActionResult PostCATEGORy(CATEGORy cATEGORy)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.CATEGORIES.Add(cATEGORy);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = cATEGORy.ID }, cATEGORy);
}
// DELETE: api/CATEGORyApi/5
[ResponseType(typeof(CATEGORy))]
public IHttpActionResult DeleteCATEGORy(int id)
{
CATEGORy cATEGORy = db.CATEGORIES.Find(id);
if (cATEGORy == null)
{
return NotFound();
}
db.CATEGORIES.Remove(cATEGORy);
db.SaveChanges();
return Ok(cATEGORy);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool CATEGORyExists(int id)
{
return db.CATEGORIES.Count(e => e.ID == id) > 0;
}
}
}
 

Answers (1)