Tuesday, January 22, 2013

Adding Custom Propeties to EF 4.0 that doesn't exist in the DB model

Hi Folks,

I was extending my EF 4.0 model by having custom propeties that don't exist in my DB, Since EF 4.0 doesn't support adding custom properties if it doesn't exist in the DB, I found a workaround for that by creating a partial class with my custom properties in it.
 

public partial class myEntityName: EntityObject

{

     
[DataMemberAttribute()]
public string customerName{ get; set; }

[DataMemberAttribute()]
public int customerCode{ get; set; }

}


Implementing this workaround will help you to add any properties you would like to add in your EF 4.0 entities.


You might encounter this error since you are adding properties to your entity that doesn't exist in the edm file:

"The number of members in the conceptual type does not match with the number of members on the object side type .Make sure the number of members are the same."

The fix for error; Remove "EdmScalarPropertyAttribute" attribute from your custom propeties:

//[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)] 
[DataMemberAttribute()]
public string customerName{ get; set; }


 Hope this helps!

No comments: