【发布时间】: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