Click here to Skip to main content
12,501,207 members (37,795 online)
Rate this:
 
Please Sign up or sign in to vote.
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
Top Experts
Last 24hrsThis month


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100