Click here to Skip to main content
11,631,896 members (69,978 online)
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 8,397
1 Sergey Alexandrovich Kryukov 8,009
2 Mika Wendelius 6,260
3 F-ES Sitecore 2,296
4 Suvendu Shekhar Giri 1,970


Advertise | Privacy | Mobile
Web02 | 2.8.150728.1 | Last Updated 28 Aug 2012
Copyright © CodeProject, 1999-2015
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