-1

We have a small old app which transfers data from one Server to another server (Using Entity Framework 5.0 and SQl server ) and generate reports issue I am having is that when it is transferring data. I am getting ("InnerException = {"String or binary data would be truncated.\r\nThe the statement has been terminated."}"..

on some data...but if I want to enter that particular data with "id" using the same app it is inserting it with no issue.I read somewhere it is an issue with EF(5.0) but I am not sure so Please let me know.

ZCoder
  • 1,731
  • 5
  • 20
  • 41
  • 1
    As far as I know it's not an issue with EF, the issue is you're trying to insert more text than _the model_ thinks the table can hold. So if your _table_ states column x is ```nvarchar(100)``` but your EF model thinks it's ```nvarchar(50)```, EF will throw this exception. Is the insert working with the same model? Or does it use a command or a Stored Procedure to insert the data? – rickvdbosch Jul 10 '17 at 13:39
  • 1
    It would be datasize issue. – karman Jul 10 '17 at 13:39
  • 1
    Possible duplicate of [SQL Server String or binary data would be truncated](https://stackoverflow.com/questions/6388756/sql-server-string-or-binary-data-would-be-truncated) – mjwills Jul 10 '17 at 13:39
  • ok, let me share you an example "I have 100 rows which I am transferring. 20 rows transferred then i got error on id '202'.. so now query that id _db.user.firstdef(c=>c.id = id ) if now i try to insert this value i will not get any issue " for this id "202" – ZCoder Jul 10 '17 at 14:15
  • 1
    Id 202 has a string that is too big. You need to `Substring` it. – mjwills Jul 10 '17 at 14:18
  • Let me check again will me back please don't close this question. – ZCoder Jul 10 '17 at 14:24
  • I re-created my .Edmx and everything working fine now :) – ZCoder Jul 10 '17 at 18:07

1 Answers1

1

You have a char / varchar / nchar / nvarchar column with a specified length. You are inserting a row where the value has a length greater than the specified length.

When you read it from the old server, you should use string.Substring to get the first X characters from the source (where X is the database column length). If you do this, your errors will stop.

mjwills
  • 21,750
  • 6
  • 36
  • 59