Custom properties for the Client Side Entity class

entity-framework

Question

I need to add a custom property to an Entity Framework class, however when I do I get the "The property name XXX specified for type XXX is not valid." error. Is there some attribute I can give the property so it is ignored and not mapped to anything?

Edit: If I add a custom property, as per Martin's example below, then the following code will raise the above error at the SaveChanges call.

MyEntities svc = new MyEntities(url);
MyEntity ent = new MyEntity();
ent.MyField = "Hello, world";
svc.AddMyEntity(ent);
svc.SaveChanges();
1
4
4/16/2011 5:09:41 AM

Accepted Answer

Here is the answer: http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/b7a9e01d-c5c2-4478-8f01-00f7f6e0f75f

Edit: A better link describes the final compact answer of Adding an Attribute to prevent serialization of the Entity when sending to the Service.

1
4/16/2011 5:06:30 AM

Popular Answer

You can add a property in code:

public partial class MyEntity {
  public String MyCustomProperty {
    get;
    set;
  }
}

The Entity Framework generate partial classes enabling you to customize the generated class.

Also, to comment on your code I think should change it to something like this:

MyEntities svc = new MyEntities(url);
// Create MyEntity using the factory method.
MyEntity ent = MyEntities.CreateMyEntity(...);
ent.MyField = "Hello, world";
svc.AddMyEntity(ent);
svc.SaveChanges();

This will ensure that your entity is properly initialized.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow