如何在Entity Framework中将字段设置为DBNull [英] How do I set a field to DBNull in Entity Framework
问题描述
如何在Entity Framework中将字段设置为DBNull?这些字段是强类型的,因此您不能将其设置为等于DBNull.Value,我没有找到任何方法将字段设置为DBNull。看来这是一件必要的事情,但经过大量的Google研究,我没有发现任何事情。
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.
我试图使用vb在Entity Framework中设置一个datetime字段。净。这需要我写
I am trying to set a datetime field in Entity Framework using vb.net. This requires me to write
myEntity.mydate = Nothing
这不会将该字段设置为空,而是将其设置为SQL Server中不是有效日期值的默认日期值。
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.
推荐答案
如果列在数据库中为空,则结果类也将具有可空属性。
If a column is nullable in the database the result class will have a nullable property too.
例如,如果订单
表具有可空的 Datetime
列,名为 CreatedDate
,那么结果订单
类将看起来像这样:
For example if a Order
table has a nullable Datetime
column called CreatedDate
, then the resulting Order
class will looks something like this:
public partial class Order: EntityObject
{
...
private DateTime? _createdDate;
...
}
如果_ createDate
当您调用 ObjectContext.SaveChanges()
a System.DBNull
将无效自动发送到数据库作为插入或更新命令的一部分。
If the _createdDate
has no value when you call ObjectContext.SaveChanges()
a System.DBNull
will automatically be sent to the database as part of insert or update command.
希望这有助于
Alex
Hope this helps Alex
这篇关于如何在Entity Framework中将字段设置为DBNull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!