在插入OData的实体所需的外键 [英] Inserting Entities in OData with required Foreign Keys

查看:298
本文介绍了在插入OData的实体所需的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改-2:之后的研究时间,几乎在谷歌转向紫色每个ODATA相关的链接,我发现的深层插入的概念(<一href="http://docs.oasis-open.org/odata/odata/v4.0/cs01/part1-protocol/odata-v4.0-cs01-part1-protocol.html#_Toc362965202"相对=nofollow>链接)存在于OData的规范。所以毕竟我在做什么应该工作,即使没有联系。有谁知道如何在微软的OData客户端上实现这一点?是否有任何其他的OData客户那里,支持这个概念?

EDIT-2: After hours of research and almost every odata related link on google turning purple, I found out that the concept of 'deep-inserts' (link) exists in the OData specification. So after all, what I'm doing should work, even without the links. Does anyone know how to enable this on the Microsoft OData client? Are there any other OData clients out there that support that concept?

编辑:也许这是错误的做法,所以请告诉我,如果我做的完全错误的。不能够节省真的阻止我们前进的步伐!

Maybe this is the wrong approach, so please tell me if I'm doing it totally wrong. Not being able to save is really blocking our progress!

我有一个问题的OData V3。我有一个类关联,有一个需要地址。当我尝试发布一个新的助理,失败是由于地址属性为空(EF6抛出DbUpdateException与外键冲突)。我的关联类看起来是这样的:

I have an issue with OData v3. I have a class Associate that has a required Address. When I try to POST a new Associate, it fails due to the Address property being null (EF6 throws DbUpdateException with foreign key violation). My Associate class looks like this:

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; }
}

我用的是微软的OData客户端,并尝试添加该联营以下列方式:

I use the Microsoft OData client, and try to add the associate in the following way:

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

在服务器,控制器,协理但是没有到达的外键。当我检查与菲德勒POST请求,我明白为什么:

On the server, in the controller, the Associate arrives without the foreign key, however. When I inspect the POST request with Fiddler, I see why:

{
    "odata.type" : "xxx.Data.Entities.Associate",
    "AddressId" : 0,
    "Id" : 0,
    "IsLegalRepresentative" : false,
    "IsMailReceiver" : false,
    "Name" : "John Doe",
    "Role" : "Father"
}

地址不传输,即使在客户端上生成的类有一个地址属性。

我该如何解决这个问题呢?

How can i solve this problem?

推荐答案

有没有解决这个。我会推出自己的上下文环境的变更集与网页API的工作的概念。我会把它放在github上,当我完成了。

There is no solution to this. I will roll my own context with a notion of change sets that works with web-api. I will put it on github, when I'm done.

这篇关于在插入OData的实体所需的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆