Entity Framework 核心数据注释数据库生成的值

Entity Framework Core Data Annotation Database Generated Values

Entity Framework Generated Properties makes it seem like Data Annotations can be used for generated code first "timestamp" 属性的 created/updated 核心文档作为 DateTime 类型。

尝试将以下数据注释与代码优先迁移一起使用时:

public class Foo {
    // some properties

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public DateTime Created { get; set; }

    // more properties
}

我尝试在命令行中执行命令 dotnet ef migrations add AddFooTimestamp 时收到以下错误:

Identity value generation cannot be used for the property 'Created' on entity type 'Foo' because the property type is 'DateTime'. Identity value generation can only be used with signed integer properties.

是否有有效的方法来利用模型文档中描述的数据注释以及 EF Core 和 SQL 服务器环境中的代码优先迁移?还是此时仅 [Timestamp] 注释可用?

我的项目正在使用工具 Microsoft.EntityFrameworkCore.Tools 版本“1.0.0-preview2-final”和 Microsoft.EntityFrameworkCore & Microsoft.EntityFrameworkCore.SqlServer 版本“1.1.0”。

感谢您提供的任何帮助。

必须 DateTimeINSERTUPDATE 操作上设置的模型属性我通过 Fluent API 配置和数据库结合使用了 Default Values触发器。我在问题中提到的注释绝对不会自动将 SQL 服务器配置为生成默认值或更新的 DateTime 值。

型号:

public class Foo {
    // some properties

    public DateTime Created { get; set; }

    public DateTime LastUpdated { get; set; }

    // more properties
}

默认值:

protected override void OnModelCreating(ModelBuilder modelBuilder)
    modelBuilder.Entity<Foo>()
        .Property(i => i.Created)
        .HasDefaultValueSql("getdate()");

    modelBuilder.Entity<Foo>()
        .Property(i => i.LastUpdated)
        .HasDefaultValueSql("getdate()");
}

更新触发器后的数据库:

CREATE TRIGGER [dbo].[Foo_UPDATE] ON [dbo].[Foo]
    AFTER UPDATE
AS
BEGIN
    SET NOCOUNT ON;

    IF ((SELECT TRIGGER_NESTLEVEL()) > 1) RETURN;

    DECLARE @Id INT

    SELECT @Id = INSERTED.Id
    FROM INSERTED

    UPDATE dbo.Foo
    SET LastUpdated = GETDATE()
    WHERE Id = @Id
END

谢谢!