c# - Dotnet Core 3.1 - 更新到 3.1 并使用 "HasDefaultValue"设置默认值似乎不存在

标签 c# entity-framework-core

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

'PropertyBuilder' does not contain a definition for 'HasDefaultValue' and no accessible extension method 'HasDefaultValue' accepting a first argument of type 'PropertyBuilder' could be found (are you missing a using directive or an assembly reference?)

发生这种情况的代码如下:

        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 时他们错过了这一点,或者他们正在使用不同的方式设置默认值..

所以在我发布这篇文章之前,我确实进行了谷歌搜索和 Stackoverflow 搜索,但没有结果。

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

最佳答案

你有没有添加https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Relational/在您的项目中为 3.1 核心打包?

我建议您安装和/或删除/安装该软件包,看看它是如何工作的。

如您在变更日志中所见,EF 3.1 中没有任何变化 here

To this end we have fixed over 150 issues for the 3.1 release, but there are no major new features to announce.

顺便说一下,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;
    }

关于c# - Dotnet Core 3.1 - 更新到 3.1 并使用 "HasDefaultValue"设置默认值似乎不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59234093/

相关文章:

c# - 在 C# 中一般创建对象

c# - 无法在 Azure DocumentDB 中进行分页

c# - 在 Windows 窗体中单击“确定”按钮时如何使焦点回到窗体?

c# - 在 C# 的 OrderBy 方法中指定参数

c# - 删除 EF Core 1.0 RC2(以前的 EF 7 RC2)中的自动增量

c# - 如何在 .NET Core 中撤消 Entity Framework Update-Database

c# - 不同命名空间中的 XAML 转换器

c# - 延迟加载导航属性返回 'System.InvalidOperationException'

c# - 如何使用大量行的插入功能测试 Entity Framework Core 数据库作为 InMemory

c# - .Net Core 一对多关系导航不起作用