String Or Binary Data Would Be Truncated - Column is Big Enough
<p>I have a confusing issue. I have a WPF application that is using a local SQL Server Database. I am using Entity Framework. I'm trying to store a byte array of length 120 into a BINARY(120) column.</p>

<p>The data should fit, but for some reason I keep getting the error, 'String or binary data would be truncated'. </p>

<p>The table structure looks like this:</p>

<pre><code>Column_name   Type        Computed   Length

Id            int         no         4
Foo           varchar     no         500
Bar           binary      no         120
</code></pre>

<p>As an experiment, I tried altering the column to be a VARBINARY(MAX) column, however even in that case I still get the same error. This would usually indicate that another column could be causing the problem, however I know the problem is not the Id column, and the string I'm storing into Foo is "test string", which is obviously small enough.</p>

<p>The method that is inserting into the database isn't doing anything special:</p>

<pre><code>public void Create(FooEntry entry)
{
    _context.FooEntries.Add(entry);
    _context.SaveChanges();
}
</code></pre>

<p>Maybe I'm missing something really obvious, but how should I go about fixing this issue?</p>

<p><strong>Update:</strong></p>

<p>Yes, I am quite sure the EDMX and the database table is in sync (because when I update the database table, I always refresh the edmx).</p>

<p>I attempted to manually insert a byte array of length 120 into the table, and did not get the error. When I manually inserted a byte array of length 121, the 'String or binary data would be truncated' error occurred, which is the correct behavior.</p>

<p>The byte array that I was attempting to insert while debugging the program was this value:</p>

<pre><code> '0x2400320061002400310030002400510078004400310072003400390079004C006D0041004A00760049004E005700730069007900490058004F007400740052007600500031006900700078004C0044007000610032004900350038007700310059002E0071003300680033004B0064002F00430074005700'
</code></pre>

<p>which is a byte array of length 120. </p>

<p><strong>Update 2:</strong></p>

<p>I ran SQL Server Profiler and it gave me the following SQL query:</p>

<pre><code>exec sp_executesql N'INSERT [dbo].[FooEntry]([Foo], [Bar])
VALUES (@0, @1)
SELECT [Id]
FROM [dbo].[FooEntry]
WHERE @@ROWCOUNT &gt; 0 AND [Id] = scope_identity()',N'@0 varchar(500),@1 binary(120)',@0='test string',@1=0x24003200610024003100300024004100420047007400560056006A0076004D006F006500770036005000300054004D002F006B007800740075007A00490036004F004400660047004A0062004E0069004E00790041007A0051006A0058004300660072007400360051007A0043004B004400580075006500
</code></pre>

<p>Very weird. This query works when I execute it manually, but EF throws an error when this query is executed while the program is running.</p>