添加数据注释到实体框架(或Linq to SQL)生成的类 [英] Add Data Annotation To Entity Framework(Or Linq to SQL) generated class
问题描述
可以添加更多数据安抚
成员,如 Range
,必需
,...到实体框架
或 Linq to SQL
自动生成的类
Is it possible to add more Data Anootation
member such as Range
,Required
,... to Entity Framework
or Linq to SQL
generated classes automatically ?
我想为我的课程使用数据注释验证
I want to use data annotation validation for my classes
谢谢
编辑1)
我为Northwind数据库创建一个实体框架模型并添加Product class.a部分代码如下:
I create an entity framework model for Northwind database and add Product class.a part of code is like this:
[EdmEntityTypeAttribute(NamespaceName="NorthwindModel", Name="Product")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Product : EntityObject
{
#region Factory Method
/// <summary>
/// Create a new Product object.
/// </summary>
/// <param name="productID">Initial value of the ProductID property.</param>
/// <param name="productName">Initial value of the ProductName property.</param>
/// <param name="discontinued">Initial value of the Discontinued property.</param>
public static Product CreateProduct(global::System.Int32 productID, global::System.String productName, global::System.Boolean discontinued)
{
Product product = new Product();
product.ProductID = productID;
product.ProductName = productName;
product.Discontinued = discontinued;
return product;
}
#endregion
#region Primitive Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 ProductID
{
get
{
return _ProductID;
}
set
{
if (_ProductID != value)
{
OnProductIDChanging(value);
ReportPropertyChanging("ProductID");
_ProductID = StructuralObject.SetValidValue(value);
ReportPropertyChanged("ProductID");
OnProductIDChanged();
}
}
}
private global::System.Int32 _ProductID;
partial void OnProductIDChanging(global::System.Int32 value);
partial void OnProductIDChanged();
我希望ProductID是必需的,但我无法以这种方式编写代码:
I want ProductID being Required but I can't write code this way:
public partial class Product
{
[Required(ErrorMessage="nima")]
public global::System.Int32 ProductID;
}
推荐答案
是的。您需要为每个实体创建第二个部分类,并将其链接到具有替代属性的辅助类。
Yes. You need to create a 2nd partial class for each entity and link it to an auxiliary class with substitute properties.
假设你有一个生成的部分类Customer {public string Name {get;组; }}
生成的类始终被标记为partial。
Suppose you have a generated partial class Customer { public string Name { get; set; } }
The generated class will always be marked as partial.
然后,您需要添加一个文件:
Then you need to add a file with :
[MetadataType(typeof(CustomerMetadata))]
public partial class Customer
{
// it's possible to add logic and non-mapped properties here
}
public class CustomerMetadata
{
[Required(ErrorMessage="Name is required")]
public object Name { get; set; } // note the 'object' type, can be anything
}
不要认为这是一个非常优雅的解决方案,但它的作品。
Personally I don't consider this a very elegant solution but it works.
这篇关于添加数据注释到实体框架(或Linq to SQL)生成的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!