Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Entity Framework Entity Property

I have an entity in EF named Profile and I would like to add data annotation attributes to the FirstName property of this entity. So, I created a new partial class like so;

public partial class Profile : EntityObject
{
    [Required]
    [Display(Name = "First Name")]
    [EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = false)]
    [DataMemberAttribute()]
    override public global::System.String FirstName
    {
        get
        {
            return _FirstName;
        }
        set
        {
            OnFirstNameChanging(value);
            ReportPropertyChanging("FirstName");
            _FirstName = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("FirstName");
            OnFirstNameChanged();
        }
    }
}

But I am getting this;

The type 'CC.Models.Profile' already contains a definition for 'FirstName'

Any ideas?

Regards,
Ryan

like image 646
RyanMAd Avatar asked Oct 11 '22 06:10

RyanMAd


1 Answers

You unfortunately can't change it like that. You have to create a metadata class and add the metadata attributes to that class. See below link:

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx

Have a look at this link to see some issues with generating a metadata class, what I normally do is if I change something I just regenerate the metadataclass by adding a new service and deleting the service afterwards and then merge the two keeping my old changes and keeping the newly added entities.

like image 89
TBohnen.jnr Avatar answered Oct 27 '22 10:10

TBohnen.jnr