[英]How can I keep using System.ComponentModel.DataAnnotations in class without change when updata Entity data model?
How can I update Model from Database and keep my models DataAnnotations without change ?如何从数据库更新模型并保持模型 DataAnnotations 不变?
Always after update model from database I need to add总是在从数据库更新模型之后我需要添加
using System.ComponentModel.DataAnnotations;
again for each class and add再次为每个班级添加
[Display(Name = "Name")]
for all fields and this take long time with every single update对于所有字段,每次更新都需要很长时间
How can I skip this step and update model from database and keep that settings ?如何跳过此步骤并从数据库更新模型并保留该设置?
Use the MetadataTypeAttribute
class to achieve the solution you are looking for.使用
MetadataTypeAttribute
类来实现您正在寻找的解决方案。
The documentation contains detailed description how to use is.该文档包含如何使用的详细说明。
As an example, let say you have an entity class Order
with the following properties:例如,假设您有一个具有以下属性的实体类
Order
:
public class Order
{
public int OrderId { get; set; }
public string OrderDescription { get; set; }
public string BillingAddress { get; set; }
… // another properties
}
The solution is to add partial
declaration to the model class and to create a second partial
class that contains the metadata:解决方案是向模型类添加
partial
声明并创建包含元数据的第二个partial
类:
using System;
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(OrderMetaData))]
public partial class Order
{
public int OrderId { get; set; }
public string OrderDescription { get; set; }
public string BillingAddress { get; set; }
… // another properties
}
Creating a meta data class with required annotations:创建具有所需注释的元数据类:
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
public partial class OrderMetaData
{
[HiddenInput(DisplayValue=false)]
public int OrderId { get; set; }
[Display(Name="Description")]
public string OrderDescription { get; set; }
[Display(Name = "Billing Address")]
public string BillingAddress { get; set; }
}
The meta data class only needs to contain properties that you want to apply annotations.元数据类只需要包含您要应用注释的属性。 Take care the entity class and the meta data class have consistent namespaces.
注意实体类和元数据类具有一致的命名空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.