繁体   English   中英

如何修复 Entity Framework Core 错误“值不能为空。参数名称:frameworkName”?

[英]How to fix Entity Framework Core error "Value cannot be null. Parameter name: frameworkName"?

使用 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, Object value)
在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean setModified, Boolean 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(Boolean acceptAllChangesOnSuccess)

在 Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)

在 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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM