如何修复实体框架核心错误"Value cannot be null. Parameter name: frameworkName"?



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

值不能为空。参数名称框架名称

这是上下文类

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-4S1AA2TSQLEXPRESS;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();
}

这是错误堆栈跟踪

at

Microsoft.EntityFrameworkCore.Metadata.Internal.ClrAccessorFactory'1.Create(PropertyInfo propertyInfo, IPropertyBase propertyBase)at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized
[TParam,TValue](TValue&target, TParam param, Func'2 valueFactory)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.WritePropertyValue(IPropertyBase propertyBase, Object value)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean setModified, Boolean isCascadeDelete, CurrentValueType valueType)at
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean setModified, Boolean isCascadeDelete)
atMicrosoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.AcceptChanges()at
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.AcceptAllChanges(IReadOnlyList'1 changedEntries)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean 接受所有更改成功)

at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean 接受所有更改成功)

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

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

at EFImplementationRepo.Program.Main() in C:\Projects\EFImplementationRepo\EFImplementationRepo\Program.cs:line 16

本周,我们将应用程序更新到 EF Core 3.1,并遇到了相同的错误消息问题。原因是,我们有没有资源库的集合导航属性。 GitHub 上已经存在一个问题。

只是想把它放在这里,以防其他人遇到这个问题。

对于.Net Framework Web应用程序来说,这也发生在我身上,这真的很奇怪,因为它只发生在我们的一个客户端上,其余的都很好。我最终发现,不起作用的那个没有设置 http 运行时目标框架,因此它在 .net 4.0 下运行,导致每次 ef core 尝试初始化设置为只读的集合时都会引发该异常。

因此,修复程序是将 http 运行时更改为我们使用的运行时 (4.7.2),并对其进行了排序。

<httpRuntime requestValidationMode="2.0" targetFramework="4.7.2" executionTimeout="600" />

目前,DbContext.SaveChanges()工作,保存数据后会引发异常。这种方式对我有用:

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

相关内容

最新更新