3

I have a entity called Product,this is a part of it's declration:

[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();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String ProductName
    {
        get
        {
            return _ProductName;
        }
        set
        {
            OnProductNameChanging(value);
            ReportPropertyChanging("ProductName");
            _ProductName = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("ProductName");
            OnProductNameChanged();
        }
    }
    private global::System.String _ProductName;
    partial void OnProductNameChanging(global::System.String value);
    partial void OnProductNameChanged();

I want to add Data Annotation to it's property .I search intenet and according to this topic : Using DataAnnotations with Entity Framework

I create a partial class this way:

[MetadataType(typeof(PersonMetaData))]
public partial class Product
{

}
public class PersonMetaData
{
    [Required(ErrorMessage = "nima", AllowEmptyStrings = false)]
    public global::System.String ProductName { set; get; }

    [Range(minimum: 10, maximum: 100, ErrorMessage = "NIIMMMAA")]
    public global::System.Int32 ProductID { set; get; }
}

but it does not work. for test I write this code:

using (NorthwindEntities ef=new NorthwindEntities())
        {
            Product p = new Product();
            p.CategoryID = 0;
            p.Discontinued = false;
            p.ProductID = 1000;
            p.ProductName = string.Empty;
            p.QuantityPerUnit = "3";
            p.SupplierID = 3;
            p.UnitsInStock = 0;
            p.UnitsOnOrder = 3;

            var context = new ValidationContext(p, serviceProvider: null, items: null);
            var results = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
            var isValid = Validator.TryValidateObject(p, context, results, true);
            if (!isValid)
            {
                foreach (var validationResult in results)
                {
                    Response.Write(validationResult.ErrorMessage);
                }
            }
        }

but isValid variable always is 'true' . where is my mistake?

thanks

Community
  • 1
  • 1
Arian
  • 12,793
  • 66
  • 176
  • 300

1 Answers1

1

Change

public class PersonMetaData
{
    [Required(ErrorMessage = "nima", AllowEmptyStrings = false)]
    public global::System.String ProductName { set; get; }

    [Range(minimum: 10, maximum: 100, ErrorMessage = "NIIMMMAA")]
    public global::System.Int32 ProductID { set; get; }
}

to

public class PersonMetaData
{
    [Required(ErrorMessage = "nima", AllowEmptyStrings = false)]
    public object ProductName { set; get; }

    [Range(minimum: 10, maximum: 100, ErrorMessage = "NIIMMMAA")]
    public object ProductID { set; get; }
}
H H
  • 263,252
  • 30
  • 330
  • 514
  • It does not work.Should I delete Field declration from first Product class?I upload my project in this address:http://www.4shared.com/file/kmmx_vRs/MetaData.html .It's one form test application.thanks – Arian Nov 02 '11 at 16:27
  • No, don't delete (edit) in generated files. Your setup looks OK and I know this works with MVC3. I'm not familiar with creating the context by hand etc. See the example here: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx – H H Nov 02 '11 at 18:53
  • As a diagnostic step, Add a `[Range(5, 10)] public int MyProperty { get; set; }` to the (now empty) Product class. If it shows up the error is in the MetaData link-up. – H H Nov 02 '11 at 18:55
  • 1
    And a little googling found this answer: http://stackoverflow.com/questions/1871499/metadatatype-problem . I didn't test but it looks like the fix you need. – H H Nov 02 '11 at 19:34
  • It works if I use orginal DataType.I think declare property as `object` not necessary – Arian Nov 05 '11 at 05:28