Reputation: 230
I have used Entity Framework 6 to create a Code First database with the following models.
public class CarOwner
{
public int Id { get; set; }
public virtual ICollection<Car> Cars { get; set; }
}
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
public int CarOwnerId { get; set; }
public virtual CarOwner CarOwner { get; set; }
public virtual CarImage CarImage { get; set; }
}
public class CarImage
{
public int Id { get; set; }
public int CarId { get; set; }
public byte[] Image { get; set; }
}
Using Visual Studio 2012 I have auto generated a "Web API 2 OData Controller with actions, using Entity Framework". This controller works as expected, and I have no errors on this part of the project.
I would like to do something like the following using a Service Reference in a client application:
var img = new CarImgage {
Image = .....
};
var car = new Car {
Name = "Ford Fiesta",
CarImage = img
};
var cars = new Collection<Car>();
cars.Add(car);
var owner = new CarOwner { Cars = cars }:
I have tried following this guide on adding a new entity, but to no avail: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/calling-an-odata-service-from-a-net-client
I know it is possible to post the "owner" object to the Web API since i can do it using a HttpClient.
I would like to know how i can do this, if it is indeed possible.
Upvotes: 1
Views: 735
Reputation: 415
After you generated service reference, In the solution explorer, you will find there is Reference.cs (path is like ProjectName/Service References/Reference.datasvcmap/Reference.cs
Then on client side, you can use the CLR object defined in this file. Code will be like:
YourServiceContainer context = new YourServiceContainer(new Uri("yourServiceRootUri"));
var img = new CarImgage {
Image = .....
};
var car = new Car {
Name = "Ford Fiesta",
CarImage = img
};
dsc.AddToCars(car);
dsc.SaveChanges();
Upvotes: 0