entity framework为NOT NULL列生成值,其默认值在db中定义

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

当我从我的代码中使用EF4添加新Customer

 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(); 

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

 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' 

并抛出错误

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

你能否告诉我如何在db级别拥有默认值的NOT NULL列不应该有EF生成的值?

D B:

 DateCreated DATETIME NOT NULL 

EF中的DateCreated属性:

  • 可空:错误
  • Getter / Setter:公开
  • 输入: DateTime
  • DefaultValue:

谢谢。

根据我对EF(最小)的了解,它不会从架构中获取默认值。 您插入行和列的事实被标记为NOT NULL,这意味着EF认为它应该为该列插入一个恰好具有DateTime.MinValue值的值。

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

EF中的表设计器的属性页上是否有任何内容可以指定默认值?

NOT NULL列表示该属性不可为空,并且它必须在您的实体中具有值。 如果未在DateTime等值类型中指定值,则将DateTime默认值。 DateTime默认值是DateTime.MinValue

据我所知,EF不支持您可以使用NOT NULL列并且不从您的应用程序发送任何值的场景。 唯一的例外是当您将属性的StoreGeneratedPattern设置为IdentityComputed但在这种情况下,您将无法在应用程序中设置该值。

因此,您可以在应用程序或数据库中设置值。 两者都没有。

您必须向ORM说,该属性是由数据库生成的。 对于上一个示例中Customer Class中的示例,以下代码应该可以使用。

 public class Customer{ [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]//The database generates a value when a row is inserted. public int CustomerId { get; set; } public string CustomerName { get; set; } public string Email { get; set; } public string Phone { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Computed)] //The database generates a value when a row is inserted or updated. public DateTime DateCreated { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.IComputed)]//The database generates a value when a row is inserted or updated. public DateTime DateUpdated { get; set; } } 

别忘了添加

 [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 

对于主键 ,由于我不知道的原因,当您在Model Class中至少使用一次DatabaseGeneratedAttribute时,您必须指定生成的选项方法。 当我使用它们时,我有一个错误,因为ORM试图在查询语句中插入密钥而不是让数据库生成它们。

有三个数据库生成选项

  • 已计算 :插入或更新行时,数据库会生成一个值。
  • 标识 :数据库在插入行时生成值。
  • :数据库不生成值。

您可以在此处找到数据库生成的选项

可能DateCreated列是smalldatetime并且需要是datetime类型吗? 尝试在Visual Studio或Management Studio中更改列类型。

  1. 将DateCreated设置为null
  2. 为INSERT添加触发器

    CREATE TRIGGER [dbo]。[ItemCreated]

    ON [dbo]。[myTable]

    插入后

    更新[dbo]。[myTable]

    SET [DateCreated] = GETDATE()

    WHERE [ID] IN(SELECT DISTINCT [ID] FROM [Inserted])AND [DateCreated]为NULL