adi res
adi res

Reputation: 105

Asp.net web api GetAll return null after doing POST

when I testing(with Fiddler Web Debugger) to insert or POST a data to the province through web api the data is inserted to the database normally but when I access the GetAll web api service the Country is returning null :

http://localhost:51372/api/province/

enter image description here

but after I doing Rebuild Solution from my VS 2013 Express For Web Edition, then access the getAll service from web api once again I got the Country is returning the value :

enter image description here

how can I get the Country is returning the value right after I doing POST data ?

this is the detail code that I use

the models :

    public class Province
    {        
        public int ProvinceId { get; set; }
        [Required]
        public string ProvinceName { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
        //Foreign Key
        [ForeignKey("Country")]
        public int CountryId { get; set; }
        //virtual
        public virtual Country Country { get; set; }
    }

    public class Country
    {
        [ScaffoldColumn(false)]
        public int CountryId { get; set; }        
        [Required]
        [Index(IsUnique=true)]
        [MaxLength(200)]
        public string CountryName { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }        
    }

Repository :

    private SelsContext db = new SelsContext();
    //get all provinces
    public IEnumerable<Province> GetAllProvince()
    {
        IEnumerable<Province> provinces = from c in db.Provinces select c;
        return provinces;
    }

    //post provinces
    public Models.Province Add(Models.Province item)
    {
        item.CreatedAt = DateTime.Now;
        item.UpdatedAt = DateTime.Now;

        db.Provinces.Add(item);
        db.SaveChanges();

        return item;
    }

controller :

// GET api/province
[Queryable]
public IQueryable<Province> Get()
{
    return repository.GetAllProvince().AsQueryable();
}

// POST api/province
public HttpResponseMessage Post([FromBody]Province value)
{
    repository.Add(value);
    var response = Request.CreateResponse<Province>(HttpStatusCode.Created, value);

    string uri = Url.Link("DefaultApi", new { id = value.ProvinceId });
    response.Headers.Location = new Uri(uri);
    return response;
}

EDIT : I've tried so far

in repository :

    public IEnumerable<Province> GetAllProvince()
    {
        IEnumerable<Province> provinces = db.Provinces.Include("Country");            
        return provinces;
    }

then

    public IEnumerable<Province> GetAllProvince()
    {
        IEnumerable<Province> provinces = db.Provinces.SqlQuery("select * from dbo.Provinces left join dbo.Countries on dbo.Countries.CountryId=dbo.Provinces.CountryId");

        return provinces;
    }

but get the same result.

any help would be much apreciated.

Upvotes: 0

Views: 676

Answers (1)

adelb
adelb

Reputation: 821

In your repository, can you do:

IEnumerable<Province> provinces = db.Provinces.Include("Country");

Upvotes: 2

Related Questions