views:

357

answers:

4

It would be really handy to be able to somehow say that certain properties in the generated entity classes should, for example, be decorated by (say) validation attributes (as well as Linq To SQL column attributes).

Is it a T4 template someplace? Or are there other ways to skin the cat?

A: 

No, the SqlMetal tool is what handles the generation of the C# and it is defined within itself how the C# is generated (or VB for that matter).

I'm not familiar with the template style you want but you could try exteding the generated classes (if they aren't that big a change) since they are just partial classes.

Otherwise you would need to write/ look for a custom implementation of SqlMetal

Slace
A: 

Unfortunately, with partial classes you cannot add attributes to a member from another part of the partial class - i.e. if SqlMetal defines property Foo, you can't add an attribute to Foo in your own half of the .cs.

This takes away one of (usually) the more powerful ways of customizing such files... you would probably have to either take a chance and hand-edit the generated file (after detaching it from the dbml completely) - or write your own dbml parser frmo scratch (mayhbe using xslt). Not easy.

Marc Gravell
+1  A: 

Damien Guard has written T4 templates that can be customized. See:

http://damieng.com/blog/2008/09/14/linq-to-sql-template-for-visual-studio-2008

...and:

http://visualstudiomagazine.com/listings/list.aspx?id=560

KristoferA - Huagati.com
A: 

The workaround in Dynamic Data is by using a metadata class which can be decorated:

[MetadataType(typeof(Product_Meta))]
 public partial class Product
 {        
   public partial class Product_Meta 
   {
     [Range(5, 50, ErrorMessage = "The product's reorder level must be greater than 5 and less than 50")]
     public object ReorderLevel { get; set; }         
   }  
 }

http://rachelappel.com/asp-net-dynamic-data/custom-validation-in-asp-net-dynamic-data-using-attributes/

Erwin