I'm a newbie starting with Odata, and I've defined an Odata Api for the following entities:
[Table("clients", Schema = "dbo")]
public class Client
{
public int Id { get; set; }
[Required]
public String Name { get; set; }
}
[Table("orders", Schema = "dbo")]
public class Order
{
public int Id { get; set; }
public DateTimeOffset Date { get; set; } = DateTimeOffset.Now;
[Required]
public virtual Client Client { get; set; }
public String Comment { get; set; }
}
The Name in the Client entity, and the Navigation Property in the Order entity are annotated as "required" because I use the Attribute Routing Api to validate the models in my controllers.
When Posting (insert) a new Order for the Client with Id 1 (which already exists in the database), I send a post request with the following json object in the body to my Odata endpoint:
{
"Date": "2017-03-16T08:28:47.700Z",
"Client": {
"Id": 1
},
"Comment": "Something"
}
and I receive the following error message:
{
"error": {
"code": "",
"message": "The request is invalid.",
"innererror": {
"message": "Order.Client.Name : Das Feld \"Name\" ist erforderlich.\r\n",
"type": "",
"stacktrace": ""
}
}
}
Translated into English, it means that the Field name for the entity Client is mandatory.
First question: how can I Post / insert an entity which has a navigation property (fk) with Odata without sending the complete object representing the navigation property in the post request, with all the fields? Is it not possible to do it sending only the id / primary key for the navigation property?
If I send the complete entity used as navigation property in the request, then the Order is created in the database, but a strange thing happens: the navigation property "Client" is ignored, and a new client with the same name is created for this inserted entity...
I mean, the order is created in the table, but the client_id for the order is not 1, because a new client was created, using the name provided. But the id was ignored...
What I'm doing wrong?
Thanks in advance.