首页 > 解决方案 > 在 EF Core Fluent 配置中使用 SqlServer HASHBYTES

问题描述

是否可以使用 HASHBYTES 作为 Entity Framework Core 中列的默认值?在插入/更新时,我想在同一个表的不同列中获取值的哈希值。

在我的配置文件中,我有这样的东西:

        builder.Property( a => a.File )
            .IsRequired();
        builder.Property(a => a.Hash)
            .HasDefaultValueSql( "HASHBYTES('SHA2_256', ??? )" )

但我不知道如何引用“文件”列。如何引用“文件”?

我试过了:

builder.Property( a => a.Hash )
                .HasDefaultValueSql( "HASHBYTES('SHA2_256', [File] )" );

但结果是

The name "File" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

标签: c#sql-serverentity-framework-core

解决方案


推荐阅读