Click here to Skip to main content
13,046,281 members (111,254 online)
Rate this:
Please Sign up or sign in to vote.

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
                return _UnitPrice;
                _UnitPrice = StructuralObject.SetValidValue(value);

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
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

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.

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.170713.1 | Last Updated 28 Aug 2012
Copyright © CodeProject, 1999-2017
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