.NET API is not inserting into SQL Server, but there are no errors

Viewed 563

I have an ASP.NET MVC application that I added Web API to in order to receive Json and insert the contents into SQL Server. On the mobile Cordova app, and in a Rest Api test, I get Http status of 200. There is however no record inserted.

Here is the class and DBContext:

public class MobileController : ApiController
{
    private PTSContext db = new PTSContext();

   //all the code....
}

I created this class to handle json body from the Http request.

public class DailyCrewActivityJson
{
    [JsonProperty("api_id")]
    public string api_id { get; set; }

    [JsonProperty("api_daily_crew_id")]
    public string api_daily_crew_id { get; set; }

    [JsonProperty("api_contract_id")]
    public string api_contract_id { get; set; }

    [JsonProperty("api_contract_task_id")]
    public string api_contract_task_id { get; set; }

}

I have this class to handle the insert.

[HttpPost]
[Route("api/mobile/insertdailycrewactivity")]
public IHttpActionResult InsertDailyCrewActivity([FromBody] DailyCrewActivityJson crewActivityJson)
{
    DailyCrewActivity d = new DailyCrewActivity();
    d.id = crewActivityJson.api_id;
    d.daily_crew_id = crewActivityJson.api_daily_crew_id;
    d.contract_id = crewActivityJson.api_contract_id;
    d.contract_task_id = crewActivityJson.api_contract_task_id;

    db.DailyCrewActivities.Add(d);
    db.SaveChanges();

    return Ok();
}

Does this pattern look ok? I try to manually execute a POST from advanced REST client and I still don't get an insert or an error.

This is what my url and my json body looks like:

https://<my url.com>/api/mobile/insertdailycrewactivity

JSON:

{
  "api_id": "74849939162039260-1569958529936",
  "api_daily_crew_id": "21273665486597612-1569862557128",
  "api_contract_id": "eef03be7-82b9-4066-93d9-cb64d346481c",
  "api_contract_task_id": "00407436-2450-4f40-9323-29860232e8f7",
}

EDIT 10/5/2019 I moved the site locally and restored the SQL Database to my local SQL Express. When I run the site and call

http://localhost/api/mobile/insertdailycrewactivity

I can step through that web api method. My error(s) are occuring in the db.SaveChanges method.

The error is from System.Collections.ListDictionaryInternal and states "{Invalid column name 'DailyCrew_id'}" I don't have a column with that name. I do have a column named 'daily_crew_id' and I have a table called 'DailyCrew' with an 'id' field. Would 'DailyCrew_id' be a name in the internal list? I don't know where to look or how to fix this.

2 Answers

It turned out that EF did not like the naming conventions I was using for tables and fields and so it was not able to establish relationships between keys. I had annotated properties as Key and Foreign Key but after I changed naming of properties to something that EF liked, then it all worked.

What happen inside

 db.DailyCrewActivities.Add(d);
 db.SaveChanges();

I believe you have implement your db insert logic in

SaveChanges()

The reason you are getting always Http status as 200 is in in your method you always returns

return Ok();

please add the SaveChanges() method implementation

Related