public void CURDCollectionNavigationPropertyAndRef() { this.TestClientContext.MergeOption = MergeOption.OverwriteChanges; Person person = new Person() { FirstName = "Sheldon", LastName = "Cooper", UserName = "******" }; this.TestClientContext.AddToPeople(person); this.TestClientContext.SaveChanges(); long? personId = person.PersonId; var startDate = DateTime.Now; Trip trip1 = new Trip() { PersonId = personId, TrackGuid = Guid.NewGuid(), ShareId = new Guid("c95d15e8-582b-44cf-9e97-ff63a5f26091"), Name = "Honeymoon", Budget = 2000.0f, Description = "Happy honeymoon trip", StartsAt = startDate, EndsAt = startDate.AddDays(3), LastUpdated = DateTime.UtcNow, }; Trip trip2 = new Trip() { PersonId = personId, TrackGuid = Guid.NewGuid(), ShareId = new Guid("56947cf5-2133-43b8-81f0-b6c3f1e5e51a"), Name = "Honeymoon", Budget = 3000.0f, Description = "Happy honeymoon trip", StartsAt = startDate.AddDays(1), EndsAt = startDate.AddDays(5), LastUpdated = DateTime.UtcNow, }; this.TestClientContext.AddToTrips(trip1); this.TestClientContext.SaveChanges(); // Create a related entity by Navigation link this.TestClientContext.AddRelatedObject(person, "Trips", trip2); this.TestClientContext.SaveChanges(); // Query Navigation properties. var trips = this.TestClientContext.People .ByKey(new Dictionary<string, object> { { "PersonId", personId } }) .Trips.ToList(); Assert.Equal(2, trips.Count()); Assert.True(trips.Any(t => t.TripId == trip1.TripId)); Assert.True(trips.Any(t => t.TripId == trip2.TripId)); // Get $ref HttpWebRequestMessage request = new HttpWebRequestMessage( new DataServiceClientRequestMessageArgs( "Get", new Uri(string.Format(this.TestClientContext.BaseUri + "/People({0})/Trips/$ref", personId), UriKind.Absolute), false, false, new Dictionary<string, string>())); using (var response = request.GetResponse() as HttpWebResponseMessage) { Assert.Equal(200, response.StatusCode); using (var stream = response.GetStream()) { StreamReader reader = new StreamReader(stream); var expectedPayload = "{" + @" ""@odata.context"":""http://*****:*****@" {" + string.Format(@" ""@odata.id"":""http://*****:*****@" ""@odata.id"":""http://localhost:18384/api/Trippin/Trips({0})""", trip2.TripId) + " }" + " ]" + "}"; var content = reader.ReadToEnd(); Assert.Equal(expectedPayload.Replace(" ", ""), content); } } // Delete $ref this.TestClientContext.DeleteLink(person, "Trips", trip2); this.TestClientContext.SaveChanges(); // Expand Navigation properties this.TestClientContext.Detach(trip1); person = this.TestClientContext.People .ByKey(new Dictionary<string, object> { { "PersonId", personId } }) .Expand(t => t.Trips) .GetValue(); Assert.Equal(1, person.Trips.Count); Assert.True(person.Trips.Any(t => t.TripId == trip1.TripId)); Person person2 = new Person() { FirstName = "Sheldon2", LastName = "Cooper2", UserName = "******" }; this.TestClientContext.AddToPeople(person2); this.TestClientContext.SaveChanges(); personId = person2.PersonId; // Add $ref this.TestClientContext.AddLink(person2, "Trips", trip2); this.TestClientContext.SaveChanges(); // Expand Navigation properties this.TestClientContext.Detach(trip1); this.TestClientContext.Detach(trip2); person = this.TestClientContext.People .ByKey(new Dictionary<string, object> { { "PersonId", personId } }) .Expand(t => t.Trips) .GetValue(); Assert.Equal(1, person.Trips.Count); Assert.True(person.Trips.Any(t => t.TripId == trip2.TripId)); }
public IHttpActionResult PostToTripsFromPeople([FromODataUri]int key, Trip trip) { var entity = DbContext.People.Find(key); if (entity == null) { return NotFound(); } if (entity.PersonId != key) { return BadRequest(); } DbContext.Trips.Add(trip); DbContext.SaveChanges(); return Created(trip); }
public void PostNonContainedEntityToNavigationProperty() { // Note that this scenario DOES NOT conform to OData spec because the client // should post a non-contained entity directly to the entity set rather than // the navigation property. This case is just to repro a customer scenario // and test if the action TrippinController.PostToTripsFromPeople works. var personId = 2; var person = this.TestClientContext.People.ByKey( new Dictionary<string, object> { { "PersonId", personId } }).GetValue(); var startDate = DateTime.Now; var trip = new Trip() { PersonId = personId, TrackGuid = Guid.NewGuid(), ShareId = new Guid("32a7ce27-7092-4754-a694-3ebf90278d0b"), Name = "Mars", Budget = 2000.0f, Description = "Happy Mars trip", StartsAt = startDate, EndsAt = startDate.AddYears(14), LastUpdated = DateTime.UtcNow, }; // By default, this line of code would issue a POST request to the entity set // for non-contained navigation property. this.TestClientContext.AddRelatedObject(person, "Trips", trip); this.TestClientContext.Configurations.RequestPipeline.OnMessageCreating = args => new HttpWebRequestMessage( new DataServiceClientRequestMessageArgs( args.Method, new Uri(string.Format(this.TestClientContext.BaseUri + "/People({0})/Trips", personId), UriKind.Absolute), // Force POST to navigation property instead of entity set. args.UseDefaultCredentials, args.UsePostTunneling, args.Headers)); var response = this.TestClientContext.SaveChanges(); Assert.Equal(201, response.Single().StatusCode); }