添加数据注解实体框架(或LINQ到SQL)生成的类 [英] Add Data Annotation To Entity Framework(Or Linq to SQL) generated class
问题描述
是否有可能更多的数据Anootation
成员添加,如范围
,必需
,...以实体框架
或 LINQ到SQL
生成的类自动?
我要使用的数据注解验证了我的课
感谢
重要提示:与此相关的话题:的与实体框架使用数据标注
修改1)
$ b验证$ b
我创建Northwind数据库的实体框架模型,并添加代码产品class.a部分是这样的:
[EdmEntityTypeAttribute(NamespaceName =NorthwindModel,名称=产品)]
[序列化()]
[DataContractAttribute(IsReference =真)]
公共部分类产品:EntityObject
{
#地区的工厂方法
///<总结>
///创建一个新的产品对象。
///< /总结>
///< PARAM NAME =产品>在产品id属性的初始值< /参数>
///< PARAM NAME =产品名称>将产品名称属性的初始值< /参数>
///< PARAM NAME =停产>该停产属性的初始值< /参数>
公共静态产品CreateProduct(全球:: System.Int32的productID,全球:: System.String产品名称,全球:: System.Boolean停产)
{
产品产品=新产品();
product.ProductID =的productID;
product.ProductName =产品名称;
product.Discontinued =停产;
收益产品;
}
#endregion
#地区的原始属性
///<总结>
///没有元数据的文档。
///< /总结>
[EdmScalarPropertyAttribute(EntityKeyProperty = TRUE,ISNULLABLE = FALSE)]
[DataMemberAttribute()]
公共全球:: System.Int32的ProductID
{
得到
{
返回_ProductID;
}
组
{
如果(_ProductID =价值!)
{
OnProductIDChanging(值);
ReportPropertyChanging(产品ID);
_ProductID = StructuralObject.SetValidValue(值);
ReportPropertyChanged(产品ID);
OnProductIDChanged();
}
}
}
私人全球:: System.Int32 _ProductID;
部分无效OnProductIDChanging(全球:: System.Int32值);
部分无效OnProductIDChanged();
我要的ProductID被要求,但我不能写代码是这样的:
公共部分类产品
{
[必需(的ErrorMessage =尼玛)]
公共全球::系统.Int32产品ID;
}
是的。您需要创建一个第二部分类为每个实体,并将其与替代性链接到一个辅助类。
假设你有一个生成的部分类客户{公共字符串名称{;组; }}
结果
生成的类将总是被标记为局部的。
然后你需要添加一个文件:
[MetadataType (typeof运算(CustomerMetadata))]
公共部分类客户
{
//它可能添加逻辑和非映射特性在这里
}
公共类CustomerMetadata
{
[必需(的ErrorMessage =名称是必需)]
公共对象名称{;组; } //注意对象的类型,可以是任何东西
}
我个人不要'T认为这是一个很优雅的解决方案,但它的工作原理。
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
thanks
Important:Related to this topic: using Metadata with Entity Framework to validate using Data Annotation
EDIT 1)
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();
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.
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到SQL)生成的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!