4

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();
3
  • What is the type of exception thrown? What happens if you delete your custom property? What if you rename it? Commented Jul 27, 2009 at 12:53
  • I get a System.Data.Services.Client.DataServiceRequestException (An error occurred while processing this request.), and inside that a System.Data.Services.Client.DataServiceClientException (The property name XXX specified for type XXX is not valid.). If I delete my custom property, or make it a function, it all works fine. If its a different name, the same problem occurs. Commented Jul 27, 2009 at 18:12
  • I can't be the only person trying to do this?? Commented Jul 29, 2009 at 7:04

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That's what I have done but that would cause me to receive the above error when using the MyEntity class.
Can you clarify when you get the error? During validation of you model, when you compile, or at run-time?
When I try to use the object to update the database, eg: MyEntities svc = new MyEntities(url); MyEntity ent = new MyEntity(); ent.MyField = "Hello, world"; svc.AddMyEntity(ent); svc.SaveChanges(); I get an exception on SaveChanges.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.