Ovi*_*Ovi 16 c# entity-framework entity-framework-core .net-core asp.net-core
在实体框架核心中设置数据库列的默认值的代码优先方法是什么?
尝试#1:在模型上使用该DefaultValue属性似乎不起作用
[DefaultValue(true)]
public bool ShowInReports { get; set; }
Run Code Online (Sandbox Code Playgroud)
运行迁移后,dotnet ef 5.0.1版本生成以下代码:
migrationBuilder.AddColumn<bool>(
name: "ShowInReports",
table: "ParametersUsed",
nullable: false,
defaultValue: false);
Run Code Online (Sandbox Code Playgroud)
尝试#2:尝试定义中的值OnModelCreating无法识别该函数:
modelBuilder.Entity<TableName>()
.Property(t=> t.ShowInReports)
.HasDefaultValue(true);
Run Code Online (Sandbox Code Playgroud)
错误:'PropertyBuilder<bool>' does not contain a definition for 'HasDefaultValue' and no accessible extension method 'HasDefaultValue' accepting a first argument of type 'PropertyBuilder<bool>' could be found (are you missing a using directive or an assembly reference?)
Riw*_*wen 14
重新发布我的评论,因为这似乎也是答案。
您需要添加Microsoft.EntityFrameworkCore.Relational包才能使HasDefaultValue扩展方法可用。
至于该DefaultValue属性,我不相信它可以与 EF Core 或一般 EF 一起使用。但我可能是错的。
| 归档时间: |
|
| 查看次数: |
6642 次 |
| 最近记录: |