Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC, Linq to SQL Data Annotation Validation

I'm trying implement Data Annotation to my Linq to SQL objects. The .dbml file is generated and I'm not sure how to add data annotation to the objects without touching the generated source code.

I tried to add data annotations to the a separate partial class of the object, but its not recognizing it, no Intelli sense either.

like image 632
Omar Avatar asked Oct 08 '09 04:10

Omar


3 Answers

Thanks,but the problem is MS define the prototype of MetadataTypeAttrubute as

[AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class MetadataTypeAttribute : Attribute

So, you had to use class but not interface


From China Forest Lee: 李晓强 [email protected] (MSN) [email protected]

like image 110
李晓强 - Li Xiaoqiang Avatar answered Nov 11 '22 23:11

李晓强 - Li Xiaoqiang


As I said in my original answer to this question, you should use an interface. The answer posted after mine (which was marked as Accepted) said to use a class. This is not as good. An interface is a better option for the following reasons:

  • If there is a mismatch between the name in your LINQ class and the name in your interface, the compiler will flag it for you
  • An interface can not be instantiated, so this protects class users from accidentally instatntiating the metadata type
  • If you use Resharper (or similar), the interface can be automatically extracted from the LINQ class
  • An interface is less verbose than an empty class
  • If you program against interfaces rather than classes (which is a good practice), then you've already got an interface you can use as your metadata type

For a class called, say "User", create an interface for it (say 'IUser'), and then update the definition of your partial User class as follows:

[MetadataType(typeof(IUser))]
public class User : IUser

Then, in your IUser interface, add appropriate Data Annotation attributes to the properties:

[Required]       
[StringLength(50, ErrorMessage = "Username cannot exceed 50 characters")]
string Username { get; set; }
like image 34
jphoward Avatar answered Nov 11 '22 22:11

jphoward


Linq to SQL generates object classes as partial. An easy way to implement data annotations is to create your own partial class of the object, place the [MetadataType(typeof(YourDataAnnotationClass))] on the partial class you created.

Example:

// Linq to SQL Class
public partial class Article 
{
   public string Title { get; set; }
   ...... etc
}

Create your own MetaData class with Metadata for each field you want to validate

public class MyMetaDataClass
{
    [Required]
    [Range(5,20)]
    public string Title { get; set; }
}

Create a Partial Class for the Object class you want to add metadata to, in this case Articles class:

[MetadataType(typeof(MyMetaDataClass))]
public partial class Article { }

Note: you don't need to specify anything in the class, just the metadata type.

like image 24
Omar Avatar answered Nov 11 '22 22:11

Omar