I am looking at Azure Mobile App and have setup a test service and client.
I've setup the following Entities service-side:
public class Hotel : EntityData
{
public string Title { get; set; }
public virtual ICollection<Booking> Bookings { get; set; }
}
public class Booking : EntityData
{
public BookingStatus BookingStatus { get; set; }
[ForeignKey("PersonId")]
public virtual Person Person { get; set; }
[ForeignKey("HotelId")]
public virtual Hotel Hotel { get; set; }
public string PersonId { get; set; }
public string HotelId { get; set; }
}
public class Person : EntityData
{
public string Name { get; set; }
public virtual ICollection<Booking> Bookings { get; set; }
}
And the controller:
public class BookingController : TableController<Booking>
{
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MobileServiceContext context = new MobileServiceContext();
DomainManager = new EntityDomainManager<Booking>(context, Request);
}
// GET tables/Booking/48D68C86-6EA6-4C25-AA33-223FC9A27959
public SingleResult<Booking> GetBooking(string id)
{
return Lookup(id);
}
// GET tables/Booking
public IQueryable<Booking> GetAllBookings()
{
return Query();
}
// PATCH tables/Booking/48D68C86-6EA6-4C25-AA33-223FC9A27959
public Task<Booking> PatchBooking(string id, Delta<Booking> patch)
{
return UpdateAsync(id, patch);
}
}
I have added some default data using CreateDatabaseIfNotExists<MobileServiceContext>
and when I startup and test the Web API, the DB gets populated and I am happy that the Keys/Relationships are setup correctly. I am just using the Code First convention naming (as per this tutorial)
I have also created a test client with the following Entities:
public class Person
{
public string Id { get; set; }
public byte[] Version { get; set; }
public string Name { get; set; }
public virtual ICollection<Booking> Bookings { get; set; }
}
public class Booking
{
public string Id { get; set; }
public byte[] Version { get; set; }
public BookingStatus BookingStatus { get; set; }
public string PersonId { get; set; }
public string HotelId { get; set; }
public virtual Person Person { get; set; }
public virtual Hotel Hotel { get; set; }
}
public class Hotel
{
public string Id { get; set; }
public byte[] Version { get; set; }
public string Title { get; set; }
public virtual ICollection<Booking> Bookings { get; set; }
}
And with this test logic:
using (var client = new MobileServiceClient(m_Url, new ODataParameterHandler())
{
client.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
client.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
var bookingTable = client.GetTable<Booking>();
var bookings = await placementTable
.Where(p => p.BookingStatus == BookingStatus.Confirmed && p.PersonId == 10)
.WithParameters(new Dictionary<string, string> { { "expand", "Hotel" } })
.ToListAsync();
var aBooking = bookings[0];
aBooking.BookingStatus = BookingStatus.Cancelled;
await bookingTable.UpdateAsync(aBooking);
}
// Class to allow $expand= querystring value to be passed in.
public class ODataParameterHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
UriBuilder builder = new UriBuilder(request.RequestUri);
builder.Query = builder.Query
.Replace("expand", "$expand")
.TrimStart('?');
request.RequestUri = builder.Uri;
return await base.SendAsync(request, cancellationToken);
}
}
The GET/ToListAsync
works ok and I get the child Hotel
object attached to my Booking
. However, the Update
fails with:
The operation failed due to a conflict: 'Violation of PRIMARY KEY constraint 'PK_dbo.Hotels'. Cannot insert duplicate key in object 'dbo.Hotels'. The duplicate key value is (0e6e1bae-bd59-46ac-9630-a2b53dd04a90).\r\nThe statement has been terminated.
But why on earth is it attemping to INSERT my child object again? Firstly, I haven't altered it, and secondly, it has an Id
, CreatedAt
etc.
I cannot find any similar issues regarding Azure Mobile Apps, but I did find this SO Post regarding Entity Framework but the OP talks about having manually created the children, so I am not sure it fully applies as I have fetched the child Entity from the DB through the TableController
.