OK here’s what’s happening. I have two apps – a WCF Data Service and an MVC application.
We’re CRUDing a type that has two collections on it. of simple types
[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; }
}
zone looks like this
[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; }
}
So the MVC site is attempting to insert the object like so
var context = new ChannelData(new Uri(ConfigurationManager.AppSettings["DataServicesUri"]));
context.AddToAllocationRules(rule);
context.SaveChanges();
and when we do so, the collection of zones is always null on the odata side, and is not empty on the MVC side.
any ideas? We cannot use fiddler, etc. to sniff out the request because we’re still in local development stage.
Update
When we use a winforms app to post back this same data type to the WCF data service, we do not see these collections either.
It seems like they are getting lost in the post request.
We are able to retrieve data from the service, and $expand() works.
Here’s the headers from 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>
I guess that you added the Zone objects to the collection on the rule object which is being added right? If that’s all you did, then this is the expected behavior. WCF DS Client doesn’t handle navigation properties changes automagicaly. You need to either:
Call context.AddObject on all the Zone objects being added and also call context.SetLink to create the relationship between the two objects.
Or you can use context.AddRelatedObject to add the Zone object and create the relationship in one go.