簡體   English   中英

不包含“HasDefaultValue”的定義

[英]Does not contain definition for "HasDefaultValue"

我想首先在代碼中為不可空字段設置默認值。

我已經包含了Microsoft.EntityFrameworkCore但我仍然

不包含“HasDefaultValue”的定義

使用 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");
        });
    }
    ...
}

英孚核心 6+

我的問題是我沒有將這個 package 添加到我的存儲庫項目中:

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

PM> Install-Package Microsoft.EntityFrameworkCore.Relational 

想出了正確的語法。 在錯誤的位置分配了默認值。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM