Search code examples
entity-frameworkwcf-data-servicesodata

Ignore non entity property when save projection changes in WCF Data Services


To reduce amount of data coming from WCF Data Services I use a projection technique.

Linq query looked like:

...
    _ctx.Files.Select(x => new FileProjection() { Id=x.Id, Name = x.Name });
...

[DataServiceKey("Id")]
public class FileProjection : ViewModelBase
{
    private string _name;

    public Guid Id { get; set; }
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            NotifyOfPropertyChange(() => Name);
        }
    }

    public string MyCustomProperty { get; set; }
}

FileProjection class has an additional property that doesn't exist in File entity model.

It works fine while getting the data. But If I tried to update Name I got an exception inside SaveChanges:

Additional information: The property 'MyCustomProperty' does not exist on type 'DataAccessLayer.Context.File'. Make sure to only use property names that are defined by the type.

Is there a way to ignore MyCustomProperty during SaveChanges?

Or XXXProjection class shouldn't have any custom public properties?

The client is Silverlight app, WCF Data Services v5.0.1, oData v3


Solution

  • Currently the only way to do this is to make the property internal or private. All public properties will be serialized and the server will then fail if it doesn't recognize it.

    Or you can use a workaround by Phani: http://blogs.msdn.com/b/phaniraj/archive/2008/12/11/customizing-serialization-of-entities-in-the-ado-net-data-services-client-library.aspx.