带有Entity Framework的SQL列默认值

Car*_*arl 14 .net entity-framework entity-framework-6

我试图使用默认SQL值的Code-First EF6.

例如,我有一个"CreatedDate"列/属性不为null,SQL的默认值为"getdate()"

我如何在我的代码模型中表示这一点?目前我有:

<DatabaseGenerated(DatabaseGeneratedOption.Computed)>
Public Property CreatedDate As DateTime
Run Code Online (Sandbox Code Playgroud)

这是否有效,或者我是否需要使用可空的内容,即使实际列不应为null,因此EF在未设置时不发送值:

<DatabaseGenerated(DatabaseGeneratedOption.Computed)>
Public Property CreatedDate As DateTime?
Run Code Online (Sandbox Code Playgroud)

或者那里有更好的解决方案吗?

我不希望EF处理我的默认设置 - 我知道这对我来说是可用的,但在我目前的情况下是不可能的.

Far*_*ina 19

目前在EF6中,没有用于定义用于特定属性默认值的数据库函数的属性.您可以对Codeplex进行投票以实现它:

https://entityframework.codeplex.com/workitem/44

实现类似的东西的公认方法是使用Computed属性Migrations指定默认数据库函数.

您的课程在C#中看起来像这样:

public class MyEntity
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime Created { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

计算属性不必是可空的.

然后,您必须运行迁移并手动修改它以包含默认的SQL函数.迁移可能如下所示:

public partial class Initial : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.MyEntities",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    Created = c.DateTime(nullable: false, defaultValueSql: "GetDate()"),
                })
            .PrimaryKey(t => t.Id);

    }

    public override void Down()
    {
        DropTable("dbo.MyEntities");
    }
}
Run Code Online (Sandbox Code Playgroud)

您会注意到defaultValueSql函数.这是使计算有效的关键


The*_*Pea 9

接受的答案对于 EF6 是正确的,我只添加了EF Core解决方案;(我的解决方案也侧重于更改默认值,而不是第一次正确创建它

中仍然没有数据属性EF Core

并且您仍然必须使用 Fluent API;它确实有一个HasDefaultValue

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>()
        .Property(b => b.Rating)
        .HasDefaultValue(3);
}
Run Code Online (Sandbox Code Playgroud)

请注意,还有HasDefaultValueSqlNULL 情况:

        .HasDefaultValueSql("NULL");
Run Code Online (Sandbox Code Playgroud)

您也可以使用迁移UpDown方法,您可以更改defaultValuedefaultValueSql但您可能需要先删除索引。下面是一个例子:

public partial class RemovingDefaultToZeroPlantIdManualChange : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropIndex(
            name: "IX_TABLE_NAME_COLUMN_NAME",
            table: "TABLE_NAME"
        );

        migrationBuilder.AlterColumn<int>(
            name: "COLUMN_NAME",
            table: "TABLE_NAME",
            nullable: true,
            //note here, in the Up method, I'm specifying a new defaultValue:
            defaultValueSql: "NULL",
            oldClrType: typeof(int));

        migrationBuilder.CreateIndex(
            name: "IX_TABLE_NAME_COLUMN_NAME",
            table: "TABLE_NAME",
            column: "COLUMN_NAME"
        );
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropIndex(
            name: "IX_TABLE_NAME_COLUMN_NAME",
            table: "TABLE_NAME"
        );

        migrationBuilder.AlterColumn<int>(
            name: "COLUMN_NAME",
            table: "TABLE_NAME",
            nullable: true,
            //note here, in the Down method, I'll restore to the old defaultValue:
            defaultValueSql: "0",
            oldClrType: typeof(int));

        migrationBuilder.CreateIndex(
            name: "IX_TABLE_NAME_COLUMN_NAME",
            table: "TABLE_NAME",
            column: "COLUMN_NAME"
        );


    }
}
Run Code Online (Sandbox Code Playgroud)