ASP。NET MVC, Linq to SQL数据注释验证。

时间:2022-02-25 16:37:41

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.

我正在尝试实现对Linq到SQL对象的数据注释。生成了.dbml文件,我不知道如何在不接触生成的源代码的情况下向对象添加数据注释。

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

我试图将数据注释添加到对象的一个单独的部分类中,但它没有识别它,也没有Intelli感。

4 个解决方案

#1


22  

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
  • 如果LINQ类中的名称与接口中的名称不匹配,编译器将为您标记它
  • 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
  • 如果您使用Resharper(或类似的),则可以从LINQ类中自动提取接口
  • 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:

对于一个名为“User”的类,创建一个接口(比如“IUser”),然后更新部分用户类的定义,如下所示:

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

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

然后,在IUser界面中,向属性添加适当的数据注释属性:

[Required]       
[StringLength(50, ErrorMessage = "Username cannot exceed 50 characters")]
string Username { get; set; }

#2


9  

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:

对于一个名为“User”的类,创建一个接口(比如“IUser”),然后更新部分用户类的定义,如下所示:

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

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

然后,在IUser界面中,向属性添加适当的数据注释属性:

[Required]       
[StringLength(50, ErrorMessage = "Username cannot exceed 50 characters")]
string Username { get; set; }

#3


6  

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.

Linq to SQL生成部分对象类。实现数据注释的一种简单方法是创建对象的部分类,将[MetadataType(typeof(YourDataAnnotationClass))]放在创建的部分类上。

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:

为要添加元数据的对象类创建一个局部类,在本例中为Articles类:

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

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

注意:不需要在类中指定任何内容,只需指定元数据类型。

#4


2  

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

谢谢,但是问题是MS定义了元数据类型的原型

[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: 李晓强 xiaoqianglinsen@163.com (MSN) lixiaoqiang@webservice.com.cn

从中国森林李:李晓强xiaoqianglinsen@163.com(MSN)lixiaoqiang@webservice.com.cn

#1


22  

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
  • 如果LINQ类中的名称与接口中的名称不匹配,编译器将为您标记它
  • 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
  • 如果您使用Resharper(或类似的),则可以从LINQ类中自动提取接口
  • 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:

对于一个名为“User”的类,创建一个接口(比如“IUser”),然后更新部分用户类的定义,如下所示:

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

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

然后,在IUser界面中,向属性添加适当的数据注释属性:

[Required]       
[StringLength(50, ErrorMessage = "Username cannot exceed 50 characters")]
string Username { get; set; }

#2


9  

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:

对于一个名为“User”的类,创建一个接口(比如“IUser”),然后更新部分用户类的定义,如下所示:

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

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

然后,在IUser界面中,向属性添加适当的数据注释属性:

[Required]       
[StringLength(50, ErrorMessage = "Username cannot exceed 50 characters")]
string Username { get; set; }

#3


6  

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.

Linq to SQL生成部分对象类。实现数据注释的一种简单方法是创建对象的部分类,将[MetadataType(typeof(YourDataAnnotationClass))]放在创建的部分类上。

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:

为要添加元数据的对象类创建一个局部类,在本例中为Articles类:

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

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

注意:不需要在类中指定任何内容,只需指定元数据类型。

#4


2  

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

谢谢,但是问题是MS定义了元数据类型的原型

[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: 李晓强 xiaoqianglinsen@163.com (MSN) lixiaoqiang@webservice.com.cn

从中国森林李:李晓强xiaoqianglinsen@163.com(MSN)lixiaoqiang@webservice.com.cn