How do you set a field to DBNull in Entity Framework? The fields are strongly typed so you cannot set it equal to DBNull.Value and I did not find any method to set a field to DBNull. It seems like this is a necessary thing to do, but after much Google research I found nothing about it.
I am trying to set a datetime field in Entity Framework using vb.net. This requires me to write
myEntity.mydate = Nothing
This does not set the field to null but instead sets it to the default date value which is not a valid date value in SQL Server.
I never used entity-framework but can you turn a variable into a nullable type?
something like
then you can do the =nothing and it will stay nothing.
but like I said, I never used entity-framework
while looking on google I found that bug report on Microsoft Connect
If a column is nullable in the database the result class will have a nullable property too.
For example if a
Order
table has a nullableDatetime
column calledCreatedDate
, then the resultingOrder
class will looks something like this:If the _
createdDate
has no value when you callObjectContext.SaveChanges()
aSystem.DBNull
will automatically be sent to the database as part of insert or update command.Hope this helps Alex
Set the field value to
null
. The Entity Framework will translate this toSystem.DBNull
.