【问题标题】:Context.UpdateObject() not passing over collections in objectContext.UpdateObject() 不传递对象中的集合
【发布时间】:2012-09-17 20:35:57
【问题描述】:

好的,这就是正在发生的事情。我有两个应用程序 - 一个 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 端始终为空,在 MVC 端不为空。

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

更新

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

他们似乎在发布请求中迷路了。

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

这是 Fiddler2 的标头

<?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>

【问题讨论】:

  • 您甚至可以在本地使用 fiddler,只要确保您使用 machinename 而不是 localhost - 它应该可以工作。除此之外,“集合在 odata 端为空”是什么意思?你是怎么检查的?
  • 抱歉标签之前没有ASP.NET MVC。使用 fiddler 更新问题以显示结果。

标签: c# asp.net-mvc wcf wcf-data-services odata


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2020-03-27
    • 2012-11-14
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 2012-08-26
    • 2014-11-04
    • 2010-10-09
    相关资源
    最近更新 更多