简体   繁体   English

更新实体数据模型时,如何在不更改的情况下继续在类中使用 System.ComponentModel.DataAnnotations?

[英]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.

相关问题 使用 System.ComponentModel.DataAnnotations 进行模型绑定 - Model binding using System.ComponentModel.DataAnnotations 为什么我不能引用 System.ComponentModel.DataAnnotations? - Why can't I reference System.ComponentModel.DataAnnotations? 使用System.ComponentModel.DataAnnotations的验证摘要; - Validation Summary using System.ComponentModel.DataAnnotations; Mono将何时支持System.ComponentModel.DataAnnotations? - When will Mono support System.ComponentModel.DataAnnotations? 找不到System.ComponentModel.DataAnnotations - System.ComponentModel.DataAnnotations was not found 为什么实现IValidatabletable的类在另一个类库中使用时需要引用System.ComponentModel.DataAnnotations? - Why classes that implements IValidatableObject requires reference to System.ComponentModel.DataAnnotations when used in another class library? ASP.NET MVC3 System.ComponentModel.DataAnnotations——注释能否确定它正在验证的类? - ASP.NET MVC3 System.ComponentModel.DataAnnotations -- can an annotation determine the class it is validating? 如何在WPF或Winforms应用程序中使用System.ComponentModel.DataAnnotations - How to use System.ComponentModel.DataAnnotations in WPF or Winforms application 如何访问字段的 System.ComponentModel.DataAnnotations 显示名称属性 - How to access the System.ComponentModel.DataAnnotations Display name property for a field 我应该添加 System.ComponentModel.DataAnnotations 作为尊敬.net 48 - Should I add System.ComponentModel.DataAnnotations as reverence .net 48
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM