views:

591

answers:

2

Hi

According to the MSDN docs, the DbType property/attribute of a Column type/element is only used for database creation.

Yet, today, when trying to submit data to an IMAGE column on a SQLCE database (not sure if only on CE), I got an exception of 'Data truncated to 8000 bytes'. This was due to the DbType still being defined as VARBINARY(MAX) which SQLCE does not support. Changing the type to IMAGE in the DbType fixes the issue.

So what other surprises does Linq2SQL attributes hold in store? Is this a bug or intended? Should I report it to MS?

UPDATE

After getting the answer from Guffa, I tested it, but it seems for NVARCHAR(10) adding a 11 char length string causes a SQL exception, and not Linq2SQL one.

The data was truncated while converting from one data type to another. 
     [ Name of function(if known) =  ]
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' 
     occurred in System.Data.SqlServerCe.dll
+1  A: 

It certainly sounds like the MSDN article could be misleading... however, LINQ-to-SQL, while still alive, isn't getting vast amounts of dev time, so I wouldn't hold my breath waiting for an update.

You could post on connect, or perhaps add a remark to the MSDN page (Community Content).

Marc Gravell
Thanks Marc :) I will continue to use and abuse Linq2SQL as long as I do not have to write SQL.
leppie
+2  A: 

The DbType is only required if you are going to create a table, but that doesn't mean that it's ignored the rest of the time.

If you for example define a VarChar column with the size 100, you will get an exception if you send a string that is longer than 100 characters even if the field in the database actually could accomodate the string.

The documentation says that you shouldn't specify the DbType if it's not needed, as the data type is inferred from the value that you use. However, there might be some situations where you don't want it to use the DbType that is inferred.

Guffa
Thanks, I didnt know that, but I swear I have never seen that behavior with VARCHAR. Will go check again :) Perhaps it was a L2S exception and not a SQL one.
leppie
That does not work :( Just tried it, getting SQL exception.
leppie
Yes, the exception comes from ADO.NET, not from LINQ to SQL, as it's ADO.NET that handles the type checking.
Guffa