Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: entity .NET4
Hi,
 
I have used a data validation in the code behind of edmx file of entity framework as:
[Range(1, 5, ErrorMessage = "Enter a value between 1 and 5")]
public global::System.Decimal UnitPrice
        {
            get
            {
                return _UnitPrice;
            }
            set
            {
                OnUnitPriceChanging(value);
                ReportPropertyChanging("UnitPrice");
                _UnitPrice = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("UnitPrice");
                OnUnitPriceChanged();
            }
        }
 
When I use this entity on consuming application then it does not check the range validation.
Posted 27-Aug-12 22:33pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

To achieve this you have to create a 2nd partial class for each entity class in EF and link it to a auxiliary class with substitute properties.
 
for example you have a generated
class Product { public int UnitPrice{ get; set; } }
The generated class will always be marked as partial.
 
and now you have to create 2nd partial class file and add
 
[MetadataType(typeof(ProductMetadata))]
public partial class Product 
{
    // it's possible to add logic and non-mapped properties here
}
 

public class ProductMetadata
{
   [Range(1, 5, ErrorMessage = "Enter a value between 1 and 5")]
    public int UnitPrice { get; set; }   // note the 'object' type
}
 
It will work.
Rate it if you find it as useful.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 278
1 Maciej Los 205
2 Nguyen.H.H.Dang 190
3 arvind mepani 187
4 Peter Leow 180


Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 28 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid