Wednesday, August 7, 2013

Creating Custom Properties for/extending the Entity framework generated class

How to create a custom property for an entity framework generated class. I had a requirement and the way I did was to create a partial class for the entity generated class. After creating the partial class, I added a property and was able to access the property in the same project.

BUT here comes problem.... This entity generated class was in a web service. After I deployed my web service and tried to test with a client application, I was not able to access the newly added custom property.


public partial class ClassA                       //original class, entity generated
    {
        public string Name { get; set; }
      
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String FROM_USER
        {
            get
            {
                return _FROM_USER;
            }
            set
            {
                OnFROM_USERChanging(value);
                ReportPropertyChanging("FROM_USER");
                _FROM_USER = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("FROM_USER");
                OnFROM_USERChanged();
            }
        }
        private global::System.String _FROM_USER;
        partial void OnFROM_USERChanging(global::System.String value);
        partial void OnFROM_USERChanged();
    }

public partial class ClassA                      //newly created partial class for the above class
    {
        public int ID { get; set; }
    }

 The above partial class didn't work for me. So I had to add an extra attribute for the new property as below:

public partial class ClassA
    {
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        public int ID { get; set; }
    }

 This solution worked for me. Hope this tip helps you.












No comments:

Post a Comment