简体   繁体   English

在字段 Entity Framework Core 中设置默认值

[英]Set Default value in field Entity Framework Core

I need to set this by Fluent API and I`ve read documentation.我需要通过 Fluent API 进行设置,并且我已经阅读了文档。 There is HasDefaultValue() method should I use.我应该使用 HasDefaultValue() 方法。 But I can not write it, VS does not give me to do it但是我不会写,VS不给我做在此处输入图像描述

the code below from here documentation下面的代码来自这里的文档

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

Check whether you have installed nuget package Microsoft.EntityframeworkCore.SqlServer .检查是否安装了 nuget package Microsoft.EntityframeworkCore.SqlServer It has a dependency of Microsoft.EntityframeworkCore.Relational whixh has the HasDefaultValue() extension它依赖于Microsoft.EntityframeworkCore.Relational whixh 具有HasDefaultValue()扩展

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 映射到 CosmosDB 集合时,有什么方法可以将默认值设置为 Entity Framework Core 实体的属性 - Is there any way to set default value to a property of Entity Framework Core entity when mapping to CosmosDB collection MySqlException:字段 'id' 没有默认值 - Entity Framework Core 2.1 - MySqlException: Field 'id' doesn't have a default value - Entity Framework Core 2.1 实体框架:设置回默认值 - Entity Framework: Set back to default value 实体框架6.1.3设置默认值 - Entity framework 6.1.3 set default value Entity Framework Core:将可空列迁移到必需时的默认值 - Entity Framework Core: default value when migrating nullable column to required 如何使用Entity Framework Core在具有defaut值的布尔值上设置另一个值? - How to set another value on a boolean with defaut value with Entity Framework Core? 如何使用 Entity Framework 6 Code First 设置默认值约束? - How can set a default value constraint with Entity Framework 6 Code First? 使用Entity Framework 6将所有记录的1字段设置为给定值 - Set 1 field for all records to a given value using Entity Framework 6 具有价值对象的实体框架核心 - Entity framework core with value objects 具有默认值的实体框架 - Entity Framework Required with Default Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM