Add Data Annotation To Entity Framework(Or Linq to SQL) generated class

女生的网名这么多〃 提交于 2019-12-22 05:06:54

问题


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;

    }

回答1:


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.



来源:https://stackoverflow.com/questions/7978608/add-data-annotation-to-entity-frameworkor-linq-to-sql-generated-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!