Search code examples
c#xamarinxamarin.formsodata

Xamarin odata POST method hitting multiple times and is using old data


I have a Xamarin project that uses odata to communicate with a web api project.

When creating a new item in the database, on the first time everything works as expected and the odata POST method is only hit one time when hitting the line below in the Xamarin view model.

await db.SaveChangesAsync();

The next time I try and save a new item the POST method is hit twice. The first time the data in the "challenge" object below, is the new data. Then somehow the POST method breakpoint gets hit again, and it has the previous items data. But, the xamarin view model SaveChangesAsync is only hit once.

public async Task<IHttpActionResult> Post(Challenge challenge)

At the end of this operation, EF saves the object successfully. But, odata throws and error: "The context is already tracking a different entity with the same resource Uri."

I really have no idea what is happening. I'm relatively new to Xamarin, and very new to odata. Is odata somehow caching previous POST data? I'm really lost and don't know how else I can try and debug this.

I've spent the last day and a half trying to google solutions, but I don't even know what to search at this point.

Info: The web project uses MVC 5 with EF The xamarin project uses Xamarin forms 5.0.0.2291 odata 7.9.0

Any help would be appreciated.


Solution

  • It turns out I was initiating a new object in both the oData controller and in a web utility

    var bar = new Foo();
    

    and

    var bar2 = new Foo();
    

    So I assume oData was tracking a context of bar and EF was tracking a context of bar2

    Once I changed that, it seems to be working as expected