0

I want to create a buddy class, for the autogenerated Designer.cs in Datafirst Entity Framework, to implement validations with dataannotations.

The solutions I found on google contains examples with simple properties like

public int EmpID
{get;set;}

whereas the properties generated in designer.cs contains some logic in it. for ex-

/// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Byte RoleID
    {
        get
        {
            return _RoleID;
        }
        set
        {
            OnRoleIDChanging(value);
            ReportPropertyChanging("RoleID");
            _RoleID = StructuralObject.SetValidValue(value);
            ReportPropertyChanged("RoleID");
            OnRoleIDChanged();
        }
    }

Please guide on how to create a buddy class for properties like the one mentioned above.

1 Answers1

0

You can use partial classes to create a buddy class for properties like the one mentioned above. Take a look at this article: Entity Framework using Partial classes to add business logic and validation to generated entities

If you need to create numerous buddy classes and there is a consistent pattern, then consider using code generation: Customize the code generated by the Entity Designer with T4 templates

Tarzan
  • 4,270
  • 8
  • 50
  • 70
  • Tarzan- Sorry but I cant access the 1st link due to web sense blocking it. and for 2nd link, I couldn't get it. Do you mean to say that I should put the dataannotations in the cs file generated by choosing the "ADO.NET EntityObject Generator" Template or Self Tracking Template. Please guide as I am novice to EF also. – VirendraTamrakar Nov 12 '12 at 21:16
  • @VirendraTamrakar instead of the 1st link, here is another article that is on StackOverflow: http://stackoverflow.com/questions/850404/using-partial-classes-in-entity-framework-with-custom-properties – Tarzan Nov 12 '12 at 21:27
  • Tarzan- The link doesnt seem to be ralted with validations or data annotations – VirendraTamrakar Nov 12 '12 at 21:42
  • It shows how to create buddy classes for Entity Framework objects by using partial classes. You can add the validation and data annotation pieces to the partial classes. – Tarzan Nov 12 '12 at 22:07