0

I'm currently building an ASP.NET Dynamic Data project with LINQ to SQL for the data access. I know that adding the DisplayName attribute to one of my properties will accomplish what I'm after, but I'd like to avoid doing this in the code that's generated by the LINQ to SQL designer.

Is there another easy way to do what I'm after or do I need to bite the bullet and just make my own metadata?

Konstantin
  • 796
  • 1
  • 11
  • 32
Sonny Boy
  • 7,848
  • 18
  • 76
  • 104
  • Don't know if you mean the following by 'bite the bullet' but since entities are generated as partial, you could create a partial class for the specific entity and use the [`MetadataTypeAttribute`](http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute(v=vs.100).aspx) If you encounter problems with that take a look at this question [MetadataType problem](http://stackoverflow.com/questions/1871499/metadatatype-problem) – Silvermind Apr 24 '13 at 21:46
  • I've already gone the route of partial classes to change the way the names of each of the tables is displayed, but I'm unsure how to so the same with each of the columns/properties as I can't duplicate them in the partial class in order to add the appropriate attributes. – Sonny Boy Apr 24 '13 at 22:05
  • Why can't you duplicate them inside a metadataclass? They are not duplicates inside the partial class, but semi-duplicates inside the metadata class. The extra partial class is just there to be decorated with `[MetadataType(typeof(EntityMetaData))]` – Silvermind Apr 24 '13 at 22:16
  • Perfect! If you'd like to answer the question, I'll mark yours as the answer. – Sonny Boy Apr 24 '13 at 22:32
  • Glad to be of help. I'll make a more complete answer. – Silvermind Apr 24 '13 at 23:31

1 Answers1

1

You can use the MetadataTypeAttribute for this as documented on MSDN.

From the documentation but for completeness (a little altered to seal the metadata inside the class):

If you have an entity Customer with a Title property, you would define the property again in the metadata class

using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;

[MetadataType(typeof(Customer.CustomerMetaData))]
public partial class Customer
{

    class CustomerMetaData
    {
         // Apply RequiredAttribute
         [Required(ErrorMessage = "Title is required.")]
         public string Title;
    }

}
Silvermind
  • 5,791
  • 2
  • 24
  • 44
  • For noobs like me: You basically make a new class with the same properties as the original class and then attach your attributes to the new class. Then, you can add the MetadataType attribute to the original class. – Sonny Boy Apr 24 '13 at 23:36
  • @SonnyBoy Yes, In addition: the method is imo even better if you use a metadata class inside the partial class with the `Customer` prefix in the attribute. – Silvermind Apr 24 '13 at 23:39