86

Is there a way I can have a generated code file like so:

public partial class A 
{
    public string a { get; set; }
}

and then in another file:

public partial class A 
{
    [Attribute("etc")]
    public string a { get; set; }
}

So that I can have a class generated from the database and then use a non-generated file to mark it up?

gunr2171
  • 10,315
  • 25
  • 52
  • 75
Chris McCall
  • 10,043
  • 8
  • 46
  • 79
  • How much is "generated from the database"? Only property definitions, or code as well? – snemarch Sep 23 '10 at 20:48
  • 1
    Short answer, no. Long answer, dup of http://stackoverflow.com/questions/456624/associate-attribute-with-code-generated-property-in-net. – Kirk Woll Sep 23 '10 at 20:51
  • @snemarch: property definitions only, I plan on doing any other code by hand. – Chris McCall Sep 23 '10 at 21:29
  • 1
    Could you do with an interface+implementation split instead of partial class? Generate the interface from the database, implement (and add attributes) in the implementation. – snemarch Sep 23 '10 at 21:44
  • yes this is possible but with the use of metadata then have the other partial inherit that metadata – CyberNinja Feb 24 '17 at 16:29

5 Answers5

90

Here is the solution I have been using for such cases. It is useful when you have auto-generated classes that you want to decorate with attributes. Let's say this is the auto-generated class:

public partial class UserProfile
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}

And let's say, I would like to add an attribute to specify that UserId is the key. I would then create a partial class in another file like this:

[Table("UserProfile")]
[MetadataType(typeof(UserProfileMetadata))]
public partial class UserProfile
{
    internal sealed class UserProfileMetadata
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
    }
}
StayOnTarget
  • 7,829
  • 8
  • 42
  • 59
Jean-François Beauchamp
  • 5,027
  • 8
  • 37
  • 74
  • 1
    Great solution. I knew that you could decorate the partial class with attributes in multiple files and even add interfaces and an inherited class to its declaration, but I didn't make the connection to the `MetadataType` attribute. Well done! – Pflugs Jan 09 '16 at 11:45
  • What if I want to add an attribute to constructor of `UserProfile`? – Botis Mar 28 '17 at 10:31
  • MetadataType can only be used for Class, what if I want to do with a Struct? – Brent81 Aug 24 '17 at 05:58
  • 2
    `MetadataType` does not exist in .NET Core – WΩLLE - ˈvɔlə Mar 05 '19 at 17:35
  • 4
    .net core uses `ModelMetadataType` – YLJ Jul 31 '19 at 06:46
  • Is there an option for .NET Core that isn't coupled to MVC? I'm writing a console app to mirror data from a legacy enterprise system to a SQL database and want to do something similar to the above but have to include a load of unwanted MVC packages for this one functionality. – Dave Aug 16 '19 at 11:18
  • 1
    @Dave Support is supposed to be coming in .NET Core 3.0 – Roger Willcocks Oct 21 '19 at 05:58
  • Also works with virtual properties (in metadata class declare property without "virtual" keyword) – menkow Apr 13 '20 at 10:08
  • @YLJ Does anyone know how to get this working in EF Core? I'm still getting the "*The invoked method is cannot be used for the entity type 'HumanResources_ResourceMaster' because it does not have a primary key.*" error even when using the `ModelMetadataType` instead of `MetadataType`. – J.D. Feb 23 '21 at 16:56
34

I've seen something like this done in an article by Scott Guthrie (near the end of it) - didn't try it myself, though.
http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

[MetadataType(typeof(Person_Validation))]
public partial class Person
{
    // Partial class compiled with code produced by VS designer
}

[Bind(Exclude="ID")]
public class Person_Validation
{
    [Required(ErrorMessage = "First Name Required")]
    [StringLength(50, ErrorMessage = "Must be under 50 characters")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Last Name Required")]
    [StringLength(50, ErrorMessage = "Must be under 50 characters")]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Age Required")]
    [Range(0, 120, ErrorMessage = "Age must be between 0 and 120")]
    public int Age { get; set; }

    [Required(ErrorMessage = "Email Required")]
    [Email(ErrorMessage = "Not a valid email")]
    public string Email { get; set; }
}
0xced
  • 21,773
  • 10
  • 89
  • 238
Dan Dumitru
  • 5,165
  • 1
  • 31
  • 43
  • 10
    This answer bears mentioning, but it is not a general solution to the question posed by the OP. Consumers of the attributes still need to know to look for the meta data class -- i.e. these attributes will *not* be returned by Attribute.GetCustomAttribute(...). (Fortunately for many use-cases, the consumers are written by MS and in certain situations this will work.) – Kirk Woll Sep 23 '10 at 21:09
  • 1
    This solution does NOT solve the problem. Why do we look to decorate members in another file? Because the class is OVERWRITTEN each time the designer runs. Thus your attribute `[MetaDataType ...` will be cleared each time the designer runs –  Nov 27 '16 at 07:05
  • 2
    @Desolator - The idea is that you don't put the `MetadataType` attribute in the file generated by the designer, you put it in the other file where the partial class `Person` is defined. – Dan Dumitru Nov 28 '16 at 12:36
  • 1
    the problem with this solution is the class shouldve been partial – CyberNinja Feb 24 '17 at 16:29
3

This is my answer
different class files or you can combine the metadatas in a same file but keep the namespace the same..so they can see each other obviously.

keep in mind when you update your model like add more columns you have to update the project class too.

--your model class
public partial class A {
    public string a {get; set;}
}

--your project class 
public class Ametadata {
     [Attribute("etc")]
     public string a {get; set;}
}


[MetadataType(typeof(Ametadata))]
public partial class A
{
}
CyberNinja
  • 815
  • 9
  • 22
1

You need to define a partial class for your A class just like below example

using System.ComponentModel.DataAnnotations;

// your auto-generated partial class
public partial class A 
{
    public string MyProp { get; set; }
}

[MetadataType(typeof(AMetaData))]
public partial class A 
{

}

public class AMetaData
{
    [System.ComponentModel.DefaultValue(0)]
    public string MyProp { get; set; }
}
Masoud Darvishian
  • 3,080
  • 4
  • 27
  • 37
0

Not as such; the compiler will complain that the member is defined in multiple parts. However, as the use of custom attributes is reflective in nature, you could define a "metadata" class and use it to contain decorators.

public class A
{
   public string MyString;
}

public class AMeta
{
   [TheAttribute("etc")]
   public object MyString;
}

...

var myA = new A();
var metaType = Type.GetType(myA.GetType().Name + "Meta");
var attributesOfMyString = metaType.GetMember("MyString").GetCustomAttributes();
KeithS
  • 65,745
  • 16
  • 102
  • 161
  • 2
    How often is it that the actor who is adding the attributes to his or her properties is also the person consuming them and will therefore know to look for the magical "Meta" classes? – Kirk Woll Sep 23 '10 at 21:01
  • Quite often, in my experience. This wouldn't work for an existing aspect-oriented framework, but if you were decorating your domain with, say, custom validation attributes, you're the one looking for them and can define where. My team has done exactly this on one of our projects. The main disadvantage is not looking for the other class; it's maintaining two parallel classes while developing, one functional, the other decorative. That would be a problem in partial classes as well, if you were able to define partial fields/properties in the first place. – KeithS Sep 23 '10 at 21:08
  • @KirkWoll It's a good question IMO. I think that makes a good argument for putting the meta classes in the same file as the annotated class, potentially even at the top of the file, so that other coders will be more likely to find that code. – Brian Sweeney May 12 '21 at 11:22