简体   繁体   English

不包含“HasDefaultValue”的定义

[英]Does not contain definition for "HasDefaultValue"

I want to set a default value for a non nullable field in code first.我想首先在代码中为不可空字段设置默认值。

I have included Microsoft.EntityFrameworkCore but I'm still getting我已经包含了Microsoft.EntityFrameworkCore但我仍然

Does not contain definition for 'HasDefaultValue'不包含“HasDefaultValue”的定义

Using EF Core 2.2.6使用 EF 核心 2.2.6

using Microsoft.EntityFrameworkCore;

public partial class MyDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Agency>(entity =>
        {
                entity.HasOne(d => d.Region)
                    .WithMany(p => p.Agencies)
                    .HasForeignKey(d => d.RegionId)
                    .HasDefaultValue(1)
                    .HasConstraintName("FK_Agency_Region");
        });
    }
    ...
}

EF Core 6+英孚核心 6+

My issue here was I didn't add this package to my Repository project:我的问题是我没有将这个 package 添加到我的存储库项目中:

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Relational/ https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Relational/

PM> Install-Package Microsoft.EntityFrameworkCore.Relational 

Figured the correct syntax. 想出了正确的语法。 Was assigning the default value at wrong place. 在错误的位置分配了默认值。

entity.Property(e => e.RegionId).HasDefaultValue(1)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM