locked
validating data using Entity Data Model RRS feed

  • Question

  • Hi,

    I am using ADO.NET Entity Framework.

    i have a table Employee which have the following fields.

    • ID int (primary key)
    • Name varchar(10) not null
    • Email varchar(30) Null

    here is the code

     

    1. EmployeeEnitites e = new EmployeeEnitites();    
    2.         Employee record = new Employee();    
    3.         record.Name = "this is my test name.........."// This produce the error because its MaxLength set to 10 in (CSDL)    
    4.         e.AddToEmployeeSets(record);    
    5.         e.SaveChanges(); // here i got the error "string or binary data would be truncated"   


     

    iwant to make automatic Length and Required Field validation in the Entity Framework.

    Is there any way? 

    Please help regarding this issue  

    Monday, May 10, 2010 4:59 PM

Answers

  • No...this is for EFv4 (.NET 4 AND VS2010).

    Please look here for an interesting article on integrating the Validation Application Block (from Enterprise Library) to EF. I haven't tested this, but I believe it works with EFv1.

    http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=46

    You may decide to do the validation at the UI(instead of in EF), till you migrate to EF4.

    Wednesday, May 12, 2010 2:12 PM

All replies

  • Yasir,

    When you look at your Model in the Designer View, you can click on one of the Properties within an Entity and then set the following values:

    Max Length = Max
    Nullable = False

    This would set the type of the field in the database to be nvarchar(MAX) and the field must not be Null. (Not sure if Max can be a numeric value since I've never needed to have that level of constraining)  If you right-click your .edmx file in Solution Explorer and select 'Open With', then select your XML Editor, you'll be able to see something like this:

     <EntityType Name="Employee">
     <Key>
      <PropertyRef Name="employeeID" />
     </Key>
     <Property Name="employeeID" Type="bigint" StoreGeneratedPattern="Identity" Nullable="false" />
     <Property Name="value" Type="nvarchar(max)" Nullable="false" />
     </EntityType>
    Is that what you need?

    Edit: Reading your post again I think I may have provided something not very helpful.  You could set up some validation on the control supplying the value to the Entity, such as a MaxLength on a TextBox or some other validation control?  Also, I'm pretty sure you could create a partial class of Employee and then do your validation on the property within the class.
    Monday, May 10, 2010 5:40 PM
  • Thanks shifty,

    I want to get the property info from XML file. In this link http://www.codeproject.com/KB/cs/LinqColumnAttributeTricks.aspx

    you will see you can get the property and their info. But i want all from XML file  because XML file set the MaxLength and Nullable values. 

    Let me know if i didn't explain my question.

    Monday, May 10, 2010 5:51 PM
  • Thanks Srinivas,

    I am using VS 2008 with .NET 3.5 SP1. Will this work on it?

    Looking to hear from you

    Tuesday, May 11, 2010 6:16 PM
  • No...this is for EFv4 (.NET 4 AND VS2010).

    Please look here for an interesting article on integrating the Validation Application Block (from Enterprise Library) to EF. I haven't tested this, but I believe it works with EFv1.

    http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=46

    You may decide to do the validation at the UI(instead of in EF), till you migrate to EF4.

    Wednesday, May 12, 2010 2:12 PM