In Entity Framework, how do I set a field to DBNull?

dbnull entity-framework

Question

How do you set a field to DBNull in Entity Framework? There is no mechanism that I could discover to set a field to DBNull since the fields are tightly typed and you cannot make them equal to DBNull.Value. Although it seems that doing this is essential, my extensive Google search turned up nothing.

I'm attempting to use VB.NET to set a datetime property in Entity Framework. This requires me to write

 myEntity.mydate = Nothing

The default date value, which is not a valid date value in SQL Server, is set instead of the column being set to null.

1
8
5/4/2009 5:54:03 PM

Accepted Answer

The result class will also contain a nullable attribute if a column is nullable in the database.

For instance, if aOrder table has a nullableDatetime named columnCreatedDate then the subsequentOrder It will look something like this in class:

public partial class Order: EntityObject
{
  ...
  private DateTime? _createdDate;
  ...
}

If thecreatedDate time you call, has no valueObjectContext.SaveChanges() a System.DBNull will be delivered to the database automatically as part of an insert or update operation.

I hope this works for Alex

15
5/5/2009 8:55:59 PM

Popular Answer

Make the field valuenull The Entity Framework will interpret this asSystem.DBNull .



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow