Setting attributes of a property in partial classes
Asked Answered
S

3

24

I have an employee class generated by Entity Framework (EF).

public partial class employee
{
    private string name;
    public string Name
    {
        get{return name;}
        set{ name = value;}
    }
}

Now I want to put a required attribute in the name property to use in for MVC3 validation in another employee partial class which is written by me in order to extend the one which is generated by EF so that I don't have to rewrite my code if I refresh the model generated by EF.

My written partial class is in the same assembly and name space.

public partial class employee
{
    // What should I write here to add required attribute in the Name property?
}
Sylphid answered 25/7, 2011 at 8:17 Comment(1)
For reference I've added a link to the two answers here as a workaround in the official MS connect article regarding this limitation. Looks like they won't support it for a long time either. connect.microsoft.com/VisualStudio/feedback/details/423437/…Dreamadreamer
S
29

It is actually possible only through buddy class but it is not recommended way. You should keep your validation in custom view model because often you need different validations for different views but your entity can keep only single set of validation attributes.

Example of buddy class:

using System.ComponentModel.DataAnnotations;

[MetadataType(typeof(EmployeeMetadata))]
public partial class Employee
{
  private class EmployeeMetadata
  {
     [Required]
     public object Name; // Type doesn't matter, it is just a marker
  }
}
Semivowel answered 25/7, 2011 at 8:45 Comment(4)
are there other reasons why this approach would not be recommended ??Degust
what happens here if the other partial also defines attributes?Recede
Note this works only with EF, if you try to fetch attributes with reflection this will not work. https://mcmap.net/q/582864/-get-custom-attributes-applied-to-generated-entities-via-metadatatype-attributeElijah
I would like to know a little bit more as to why this is "not a recommended way". I'm using EF6, Code First, along with an external generator for the gen'd partial class. Your pattern seems to work great on the hand-coded partial class.Oculist
Y
9

You can't, as far as I'm aware - it's just not feasible.

You should possibly look to see whether MVC3 has any way of adding attributes elsewhere (e.g. to the type) which relate to another property.

Alternatively, you could add a proxying property:

[ValidationAttributesHere]
public string ValidatedName
{
    get { return Name; }
    set { Name = value; }
}
Yellowgreen answered 25/7, 2011 at 8:21 Comment(0)
H
0

Another way to do this is:

private class EmployeeMetadata
{
    //the type HAS to match what your have in your Employee class
    [Required]
    public string Name { get; set; }
}

public partial class Employee : EmployeeMetadata
{
}

At least this worked with Linq to SQL. However I had trouble accessing the attributes through GetCustomAttributes (even using System.Attribute.GetCustomAttributes didn't seem to help). Nonetheless MVC did respect those attributes. Additionally this will not work with inheriting from interfaces. Passing attributes from interface will only work using MetadataType class attribute (see answer by Ladislav Mrnka).

Hegarty answered 26/9, 2014 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.