相关疑难解决方法(0)

实体框架为NOT NULL列生成值,其默认值在db中定义

嗨,我有一个表客户.表中的一列是DateCreated.此列是NOT NULL但在db中为此列定义了默认值.

当我Customer从我的代码中使用EF4 添加新内容时.

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();
Run Code Online (Sandbox Code Playgroud)

上面的代码生成以下查询.

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'
Run Code Online (Sandbox Code Playgroud)

并抛出错误

The conversion of a datetime2 data type to a datetime data type resulted in an …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-4

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

c# ×1

entity-framework-4 ×1