使用EF Core的预发行版(3.0.0-preview6.19304.10)。当我致电DbContext.SaveChanges()
时,会导致错误
值不能为null。参数名称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参数,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更改条目)
在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)at 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
答案 0 :(得分:5)
我们本周将应用程序更新为EF Core 3.1,并遇到了相同错误消息的问题。原因是,我们具有不带setter的集合导航属性。 GitHub上已经存在问题。
答案 1 :(得分:0)
当前,DbContext.SaveChanges()
在工作,保存数据后引发异常。这样对我有用:
void CallSaveChanges()
{
try
{
_baseRepository.SaveChanges();
}
catch (ArgumentNullException) { }
}