Get a table data from database using WebApi & Entity Framework 6.XX

asp.net-web-api2 c# entity-framework-6 sql-server

Question

I created an entity data model using this database table. enter image description here

Then add 4 lines of code to WebApiConfig.cs to get the response's JSON data.

var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        config.Formatters.Remove(config.Formatters.XmlFormatter);

Then I constructed a Web API 2 Controller called importController with read/write actions.

the following programmes to add and retrieve data from the database. but it doesn't.

    CodeXenETSEntities db = new CodeXenETSEntities();  

    [HttpPost]
    [ActionName("pushlocation")]
    // POST: api/pushlocation  
    public HttpResponseMessage pushlocation( int userid,decimal longitude, decimal latitude , TimeSpan time )
    {
        user_locations ulog = new user_locations();
        ulog.user_id = userid;
        ulog.lon = longitude;
        ulog.lat = latitude;
        ulog.time = time;
        db.user_locations.Add(ulog);
        db.SaveChanges();
        return Request.CreateResponse(HttpStatusCode.Accepted, "Successfully Created");
    }


    [HttpGet]
    [ActionName("pulllocation")]
    // GET: api/pulllocation/5  
    public HttpResponseMessage pulllocation(int userid, decimal longitude, decimal latitude, TimeSpan time)
    {

        db.user_locations.ToList();
        return Request.CreateResponse(HttpStatusCode.Accepted, "Success");
    }

Here is the result:enter image description here

Database Information

enter image description here

1
2
9/15/2016 7:58:24 PM

Accepted Answer

ZZZ_tmp
1
9/16/2016 1:37:57 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow