【问题标题】:How to fix Entity Framework Core error "Value cannot be null. Parameter name: frameworkName"?如何修复实体框架核心错误“值不能为空。参数名称:frameworkName”?
【发布时间】:2019-11-26 06:54:59
【问题描述】:

使用 EF Core 的预发布版本 (3.0.0-preview6.19304.10)。当我调用DbContext.SaveChanges() 时,它会导致错误

值不能为空。参数名称frameworkName

这里是上下文类

using EFPersistence.Configurations;
using EFPersistence.Models;
using Microsoft.EntityFrameworkCore;

namespace EFPersistence.Contexts
{
    public class EFContext : DbContext
    {
        public EFContext() : base()
        {
        }

        public DbSet<ISRSetting> ISRSettings { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-4S1AA2T\SQLEXPRESS;Initial Catalog=TestEF;Integrated Security=True");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new ISRSettingsConfiguration());
            modelBuilder.ApplyConfiguration(new ISRDemographicSettingsConfiguration());
        }
    }
}

模型配置:

public class ISRSettingsConfiguration : IEntityTypeConfiguration<ISRSetting>
{
    public void Configure(EntityTypeBuilder<ISRSetting> builder)
    {
        // PK
        builder.HasKey(p => p.Id).ForSqlServerIsClustered();
        builder.Property(p => p.Id)
            .ValueGeneratedOnAdd();

        builder.ToTable("ISRSettings");
    }
}

public class ISRDemographicSettingsConfiguration : IEntityTypeConfiguration<ISRDemographicSetting>
{
    public void Configure(EntityTypeBuilder<ISRDemographicSetting> builder)
    {
        // PK
        builder.HasKey(p => p.Id).ForSqlServerIsClustered();
        builder.Property(p => p.Id)
            .ValueGeneratedOnAdd();

        builder.ToTable("ISRSettingsDemographics");

        // FK
        builder
            .HasOne(p => p.ISRSettings)
            .WithMany(p => p.DemographicsSettings)
            .HasForeignKey(p => p.ISRSettingsId)
            .OnDelete(DeleteBehavior.Cascade);
    }
}

还有实体:

        static void SeedData(EFBaseRepository<EFContext, ISRSetting, ISRSetting, int> baseRepository)
        {
            Console.WriteLine("Adding 1 entity");
            baseRepository.Add(CreateISRSettings(0)); // Works: 
            Console.WriteLine("Added 1 entity");
            baseRepository.SaveChanges();
        }

这是错误堆栈跟踪

在 Microsoft.EntityFrameworkCore.Metadata.Internal.ClrAccessorFactory'1.Create(PropertyInfo propertyInfo, IPropertyBase propertyBase)
在 Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue&target, TParam param, Func'2 valueFactory)
在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.WritePropertyValue(IPropertyBase propertyBase, 对象值)
在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase,对象值,布尔 setModified,布尔 isCascadeDelete,CurrentValueType valueType)
在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean setModified, Boolean isCascadeDelete)
在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.AcceptChanges()
在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.AcceptAllChanges(IReadOnlyList'1 changedEntries)
在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(布尔 接受AllChangesOnSuccess)

在 Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean 接受AllChangesOnSuccess)

在 EFPersistence.Repositories.EFBaseRepository'4.SaveChanges() 中 C:\Projects\EFImplementationRepo\EFPersistance\Repositories\EFBaseRepository.cs:line 154

在 EFImplementationRepo.Program.SeedData(EFBaseRepository'4 baseRepository) 在 C:\Projects\EFImplementationRepo\EFImplementationRepo\Program.cs:line 31

在 EFImplementationRepo.Program.Main() 中 C:\Projects\EFImplementationRepo\EFImplementationRepo\Program.cs:line 16

【问题讨论】:

  • 你的实体和地图cs文件在哪里?
  • 已添加到问题中
  • 但我看不到实体?
  • 如何解决?预发布(测试版)软件预计会出现问题并且无法正常运行。解决方案很简单——不要使用预发布软件,切换到最新的稳定 EF Core 版本。

标签: c# entity-framework .net-core ef-core-3.0


【解决方案1】:

目前DbContext.SaveChanges()正在工作,保存数据后抛出异常。这种方式对我有用:

void CallSaveChanges()
{
    try
    {
        _baseRepository.SaveChanges();
    }
    catch (ArgumentNullException) { }
}

【讨论】:

  • 只是为了解释为什么这对任何可能没有意识到它的人来说非常糟糕,这只是让命令失败,捕获错误并默默地忽略它。因此,该应用程序不仅不再工作,它也不会通知您出现问题。
  • 我们永远不应该捕获异常并且什么都不做。
【解决方案2】:

本周我们将应用程序更新为 EF Core 3.1,但遇到了同样错误消息的问题。原因是,我们有没有设置器的集合导航属性。 GitHub已经有问题了。

【讨论】:

  • 谢谢!在从 3.1降级到 2.2 时,出于同样的原因,我遇到了同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多