更新到Dotnet Core 3.1并移植我的代码,我发现在DbContext中出现以下错误:


  “ PropertyBuilder”不包含以下定义
  “ HasDefaultValue”,并且没有可访问的扩展方法“ HasDefaultValue”
  接受类型为“ PropertyBuilder”的第一个参数可能是
  找到(您是否缺少using指令或程序集引用?)


我发生此情况的代码如下:

        modelBuilder.Entity<Tenant>().Property(t => t.TenantNo).HasMaxLength(20);
        modelBuilder.Entity<Tenant>().Property(t => t.CompanyName).HasMaxLength(100).IsRequired();
        modelBuilder.Entity<Tenant>().Property(t => t.ContactLastName).HasDefaultValue(false).IsRequired();
        modelBuilder.Entity<Tenant>().Property(t => t.Email).HasMaxLength(500).IsRequired();
        modelBuilder.Entity<Tenant>().Property(t => t.MobilePhone).HasMaxLength(20).IsRequired();
        modelBuilder.Entity<Tenant>().Property(t => t.OfficePhone).HasMaxLength(20);
        modelBuilder.Entity<Tenant>().Property(t => t.CompanyEmail).HasMaxLength(500);
        modelBuilder.Entity<Tenant>().Property(t => t.Address1).HasMaxLength(500);
        modelBuilder.Entity<Tenant>().Property(t => t.Address2).HasMaxLength(500);
        modelBuilder.Entity<Tenant>().Property(t => t.ABN).HasMaxLength(14);
        modelBuilder.Entity<Tenant>().Property(t => t.Database).HasMaxLength(100).IsRequired();
        modelBuilder.Entity<Tenant>().Property(t => t.IsLocked).HasDefaultValue(false);


我曾经使用过.HasDefaultValue的地方,都收到此错误。我相信,我拥有所有必需的指令...

using JobsLedger.CATALOG.Entities;
using Microsoft.EntityFrameworkCore;
using System.Threading;
using System.Threading.Tasks;


似乎从3.0升级到3.1时,他们错过了这一点,或者他们使用其他方式来设置默认值。

因此,在我发布此内容之前,我确实做了Google搜索以及没有结果的Stackoverflow搜索。

想知道有人会建议如何在3.1中设置默认值吗?

最佳答案

您是否在项目中为3.1核心添加了https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Relational/软件包?

我建议您安装和/或删除/安装软件包,并查看其工作原理。

正如您在变更日志here中所看到的,EF 3.1中没有任何更改


  为此,我们为3.1发行版修复了150多个问题,但没有要宣布的主要新功能。


顺便说一下,在包中的this is the current code for 3.1处,您可以看到扩展的存在。

    public static PropertyBuilder HasDefaultValue(
        [NotNull] this PropertyBuilder propertyBuilder,
        [CanBeNull] object value = null)
    {
        Check.NotNull(propertyBuilder, nameof(propertyBuilder));

        propertyBuilder.Metadata.SetDefaultValue(value ?? DBNull.Value);

        return propertyBuilder;
    }

09-18 17:07