gpt4 book ai didi

c# - 如何修复 Entity Framework Core 错误 "Value cannot be null. Parameter name: frameworkName"?

转载 作者:行者123 更新时间:2023-12-02 03:13:01 26 4
gpt4 key购买 nike

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

Value cannot be null. Parameter name 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();
}

这是错误堆栈跟踪

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)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.AcceptChanges()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.AcceptAllChanges(IReadOnlyList'1 changedEntries)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)

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

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,并遇到了具有相同错误消息的问题。原因是,我们有没有 setter 的集合导航属性。GitHub 上已经存在问题.

关于c# - 如何修复 Entity Framework Core 错误 "Value cannot be null. Parameter name: frameworkName"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57075861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com