实体框架为非NULL列生成值,它在db中定义了默认值 [英] Entity framework generates values for NOT NULL columns which has default defined in db

查看:129
本文介绍了实体框架为非NULL列生成值,它在db中定义了默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子客户。表中的一列是 DateCreated 。此列是 NOT NULL ,但是为db中的此列定义了默认值。



当我添加新的客户使用我的代码中的EF4。

  var customer = new Customer(); 
customer.CustomerName =Hello;
customer.Email =hello@ello.com;
//注意注释掉。
//customer.DateCreated = DateTime.Now;
context.AddToCustomers(customer);
context.SaveChanges();

以上代码生成以下查询。

  exec sp_executesql N'insert [dbo]。[客户]([CustomerName],
[Email],[Phone],[DateCreated],[DateUpdated])
值(@ 0,@ 1,null,@ 2,null)
从[dbo]中选择[CustomerId]
[客户]
其中@@ ROWCOUNT> 0和[CustomerId] = scope_identity()
',N'@ 0 varchar(100),@ 1 varchar(100),@ 2 datetime2(7)
',@ 0 ='Hello' @ 1 ='hello @ ello.com',@ 2 ='0001-01-01 00:00:00'

并抛出以下错误

 将datetime2数据类型转换为datetime数据类型导致out-范围值。 
该语句已被终止。

你能告诉我,如何在db级别中具有默认值的NULL列不应该生成值通过EF?



DB:

  DateCreated DATETIME NOT NULL 

EFC中的DateCreated属性:




  • Nullable: False

  • Getter / Setter: public

  • 类型: DateTime

  • DefaultValue:



谢谢。

解决方案

根据我对EF(这是最小)的知识,它不会从模式中获取默认值。您插入行和列的事实标记为NOT NULL,表示EF认为应该为该列插入一个恰好具有DateTime.MinValue值的值。



您可能需要在实体构造函数中强制自己的值或创建一些工厂方法。



表设计器的属性页上是否有任何内容EF允许您指定默认值?


Hi I have a table Customer. One of the columns in table is DateCreated. This column is NOT NULL but default values is defined for this column in db.

When I add new Customer using EF4 from my code.

var customer = new Customer();
customer.CustomerName = "Hello";                
customer.Email = "hello@ello.com";
// Watch out commented out.
//customer.DateCreated = DateTime.Now;
context.AddToCustomers(customer);
context.SaveChanges();

Above code generates following query.

exec sp_executesql N'insert [dbo].[Customers]([CustomerName], 
[Email], [Phone], [DateCreated], [DateUpdated])
values (@0, @1, null, @2, null)
select [CustomerId]
from [dbo].[Customers]
where @@ROWCOUNT > 0 and [CustomerId] = scope_identity()
',N'@0 varchar(100),@1 varchar(100),@2 datetime2(7)
',@0='Hello',@1='hello@ello.com',@2='0001-01-01 00:00:00'

And throws following error

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

Can you please tell me how NOT NULL columns which has default values at db level should not have values generated by EF?

DB:

DateCreated DATETIME NOT NULL

DateCreated Properties in EF:

  • Nullable: False
  • Getter/Setter: public
  • Type: DateTime
  • DefaultValue: None

Thanks.

解决方案

From my knowledge of EF (which is minimal), it does not grab the default value from the schema. The fact that you are inserting the row and the column is marked as NOT NULL, means EF thinks it should be inserting a value for that column which happens to have the value of DateTime.MinValue.

You may need to force your own values in the entities constructor or create some factory methods.

Is there anything on the property pages of the table designer in EF that lets you specify a default value?

这篇关于实体框架为非NULL列生成值,它在db中定义了默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆