views:

1682

answers:

5

I have generated linq to sql entites but cannot figure out how to assign null to a nullable column. whenever i try to assign null to it it says "there is no implicit type conversion between int and ". BTW the type of the field is int? and the database column is also nullable.

A: 

It seems as though the column isn't really nullable. Check the properties of the column and see that it really is marked as nullable. If it isn't, check your database model and try to recreate the LINQ to SQL model if it's nullable in the database.

Mind that you can't simply mark it as nullable in the LINQ to SQL model and not in the database, since these kinds of discrepancies may cause your LINQ to SQL model to stop working.

Post edit update: I can see that the field type is int? which is the same as Nullable<int> so there shouldn't be a problem to set it to null. However, when getting the integer value, you should use the Value property of the int?. Is that the problem?

Omer van Kloeten
thats all in place
Addi
+1  A: 

Try to assign it System.DBNull instead

Marcus King
or Systm.DBNull.Value
DOK
A: 

Usually I've had to use DBNULL.Value when assigning or comparing a database value to null.

JettGeek
A: 

You only need to assign then entites nullable property to null. I've never had to assign or test against System.DBNull when using linq-to-sql entities.

It sounds like the generated entity classes have been manually modified or are out of date from the database.

For your reference, the following is a nullable and a non-nullable integer field:

private int? _someID;
/// <summary>
/// Gets or sets the SomeID.
/// </summary>
[Column(Name="SomeID", Storage="_someID", DbType="INT", UpdateCheck=UpdateCheck.Never)]
public int? SomeID
{

private int _someOtherID;
/// <summary>
/// Gets or sets the SomeOtherID.
/// </summary>
[Column(Name="SomeOtherID", Storage="_someOtherID", DbType="INT NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
public int SomeOtherID
{

Check your generated entity looks something like the above.

Robert Paulson
A: 

You wrote 'the type of the field is int?'.
I guess this is the problem: the type of the field should be int (without the ?), and the property 'allow null value' should be set to true.

In the designer you can change the field type to int? instead of int and 'allow null', but you run into all kind of trouble this way (as you might have noticed).

Sam