none
How to handle extra characters trying to insert in database table RRS feed

  • Question

  • I am new to Entity framework!

    Here in my application, I have set MaxLength to 20 for EmpName column/property.

    And when I try to insert EMpName having 30 characters, I want to allow first 20 characters to insert and remaining 10 characters should be skipped. Can I do it through some in-build functionality in EF. I don't want to first do the trim using some code and then insert the trimmed value in DB table.

    Kindly let me know the correct way to accomplish this.

    Friday, March 11, 2016 12:15 PM

Answers

  • Use Substring to truncate

    EmpName = Truncate(EmpName, 20);

    public static string Truncate2(string source, int length)
        {
    	return source.Substring(0, Math.Min(length, source.Length));
        }

    Before you Add the Employee Object in your Business Logic Layer.

    chanmm


    chanmm

    Sunday, March 13, 2016 10:26 PM

All replies

  • And when I try to insert EMpName having 30 characters, I want to allow first 20 characters to insert and remaining 10 characters should be skipped. Can I do it through some in-build functionality in EF. I don't want to first do the trim using some code and then insert the trimmed value in DB table.

    Well that's what you have to do, because there is nothing in EF that's going to do it for you.

    Friday, March 11, 2016 1:05 PM
  • Use Substring to truncate

    EmpName = Truncate(EmpName, 20);

    public static string Truncate2(string source, int length)
        {
    	return source.Substring(0, Math.Min(length, source.Length));
        }

    Before you Add the Employee Object in your Business Logic Layer.

    chanmm


    chanmm

    Sunday, March 13, 2016 10:26 PM