2

I have a problem with Entity Framework, it's truncating a string value while saving into database

I have database column Description nvarchar(max) in SQL Server 2014, and a property like this on my model class:

public string Description { get; set; }

I used this code to insert into database

db.EnitityName.Add(object);
db.SaveChanges();

Normally it works to save records, there is no problem, but when string exceeds more than 40k characters in Description property, it's saved value is truncated to the last characters from 40k to above.

E.g we have a 50k characters string, it saves only the first 40k characters and truncates the last 10k characters.

Is there something I did wrong, or does Entity Framework have a limitation on the string length, or database limitation in nvarchar(max)?

Please help me, I appreciate your valuable time in advance.

Thanks

10
  • 2
    How do you determine the rest is lost? There is no limitation in EF for string lengths. Commented Apr 11, 2017 at 3:53
  • thanks, good point, when i see database value its not complete string, its truncated. that is the problem, if i send 2-3k character string its save all in Database, you may get better idea now. Commented Apr 11, 2017 at 4:19
  • 3
    But how do you check for it? Querying database directly with a tool? Another C# code? Something else? What does simply asking for the length do? Commented Apr 11, 2017 at 4:21
  • by using select statement in sql query or directly open in database. both way i check. Commented Apr 11, 2017 at 4:23
  • 2
    Are you selected in Sql Server Management studio? that does have a length limit on things you select. try select len(Description) from EnitityName and check the size that way. Commented Apr 11, 2017 at 5:29

1 Answer 1

10

I had the same issue, but it turned out to be SQL Server Management Studio truncating the data, not Entity Framework (as Scott Chamberlain said in the comments). Change it by going to Query->Query Options):

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Had similar issue. Was saving some 356k chars to a column and when queried - we'd also just see ~65XXX chars. Above fix works perfectly. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.