使用所需的外键在OData中插入实体

Lue*_*eTm 6 .net c# odata asp.net-web-api

EDIT-2: 经过数小时的研究和几乎所有与谷歌相关的链接变成紫色,我发现OData规范中存在"深插入"(链接)的概念.毕竟,即使没有链接,我正在做的事也应该有效.有谁知道如何在Microsoft OData客户端上启用它?有没有其他OData客户支持这个概念?

编辑:也许这是错误的方法,所以请告诉我,如果我做错了.无法保存真的阻碍了我们的进步!

我有OData v3的问题.我有一个Associate有必要的课程Address.当我尝试POST一个新的Associate时,由于该Address属性为null(EF6因外键违反而抛出DbUpdateException)而失败.我的Associate班级看起来像这样:

public class Associate
{
    public int Id { get; set; }

    [Required, StringLength(100)]
    public string Name { get; set; }

    [Required, StringLength(50)]
    public string Role { get; set; }

    public bool IsMailReceiver { get; set; }
    public bool IsLegalRepresentative { get; set; }

    [ForeignKey("AddressId")]
    public virtual Address Address { get; set; }
    public int AddressId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我使用Microsoft OData客户端,并尝试以下列方式添加关联:

var associate = new Associate { /* ... */ };
context.AddObject("Associates", associate);
context.AddObject("Addresses", associate.Address);

/* UI fills associate data */

context.SetLink(associate, "Address", associate.Address);
context.UpdateObject(associate);
context.UpdateObject(associate.Address);

/* at this point the associate has the address set! */

context.SaveChanges(); // << Exception
Run Code Online (Sandbox Code Playgroud)

但是,在服务器上,在控制器中,Associate在没有外键的情况下到达.当我用Fiddler检查POST请求时,我明白了原因:

{
    "odata.type" : "xxx.Data.Entities.Associate",
    "AddressId" : 0,
    "Id" : 0,
    "IsLegalRepresentative" : false,
    "IsMailReceiver" : false,
    "Name" : "John Doe",
    "Role" : "Father"
}
Run Code Online (Sandbox Code Playgroud)

即使客户端上生成的类具有Address属性,也不会传输地址.

我怎么解决这个问题?

Lue*_*eTm 0

对此没有解决办法。我将使用与 web-api 一起使用的变更集概念来滚动我自己的上下文。完成后我会将其放在 github 上。