gpt4 book ai didi

c# - Context.UpdateObject() 不传递对象中的集合

转载 作者:太空宇宙 更新时间:2023-11-03 13:55:15 25 4
gpt4 key购买 nike

好的,这是正在发生的事情。我有两个应用程序 - 一个 WCF 数据服务和一个 MVC 应用程序。

我们正在 CRUD 一个有两个集合的类型。简单类型

    [DataServiceKey("Id")]
[ETag("ModifiedDate")]
public class AllocationRule
{

public AllocationRule()
{
Zones = new List<Zone>();
}

public int Id { get; set; }

public string Name { get; set; }

public List<Zone> Zones { get; private set; }
}

区域看起来像这样

    [DataServiceKey("Code")]
[EntityPropertyMapping("Name", SyndicationItemProperty.Title, SyndicationTextContentKind.Plaintext, true)]
[EntityPropertyMapping("ModifiedDate", SyndicationItemProperty.Updated, SyndicationTextContentKind.Plaintext, true)]
[ETag("ModifiedDate")]
public class Zone
{
public string Code { get; set; }

public string Name { get; set; }

public DateTime? ModifiedDate { get; set; }
}

所以 MVC 站点正试图像这样插入对象

var context = new ChannelData(new Uri(ConfigurationManager.AppSettings["DataServicesUri"]));
context.AddToAllocationRules(rule);
context.SaveChanges();

当我们这样做时,区域集合在 odata 端始终为 null,并且在 MVC 端不为空。

有什么想法吗?我们还不能使用fiddler等来嗅探请求,因为我们还处于本地开发阶段。

更新

当我们使用 winforms 应用将相同的数据类型发回 WCF 数据服务时,我们也看不到这些集合。

看起来他们在帖子请求中迷路了。

我们能够从服务中检索数据,并且 $expand() 有效。

这是来自 Fiddler2 的 header

<?xml version="1.0" encoding="utf-8"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><category term=" Data.Services.Entities.AllocationRule" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<id /><title type="text">dfsdsdf</title><published>0001-01-01T00:00:00-05:00</published>
<updated>2012-09-18T15:44:36-04:00</updated><author><name />
</author><content type="application/xml"><m:properties><d:Amount m:type="Edm.Int32">50</d:Amount><d:CreatedBy m:null="true" />
<d:CreatedDate m:type="Edm.DateTime">0001-01-01T00:00:00</d:CreatedDate><d:EffectiveEndDate
m:type="Edm.DateTime">2012-12-18T15:44:30.1686832-05:00</d:EffectiveEndDate><d:EffectiveStartDate
m:type="Edm.DateTime">2012-09-19T15:44:30.1696833-04:00</d:EffectiveStartDate><d:Enabled
m:type="Edm.Boolean">true</d:Enabled><d:Finalized m:type="Edm.Boolean">false</d:Finalized><d:Id
m:type="Edm.Int32">0</d:Id><d:ModifiedBy m:null="true" /><d:ModifiedDate m:type="Edm.DateTime"
m:null="true" />
<d:Name>dfsdsdf</d:Name><d:UnitOfMeasure>hours</d:UnitOfMeasure></m:properties></content></entry>

最佳答案

我猜您是将 Zone 对象添加到正在添加的规则对象的集合中,对吗?如果这就是您所做的全部,那么这是预期的行为。 WCF DS 客户端不会自动处理导航属性更改。您需要:

在添加的所有区域对象上调用 context.AddObject 并调用 context.SetLink 来创建两个对象之间的关系。

或者您可以使用 context.AddRelatedObject 添加 Zone 对象并一次性创建关系。

关于c# - Context.UpdateObject() 不传递对象中的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12466719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com