使用 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)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean setModified, Boolean isCascadeDelete, CurrentValueType valueType)
在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.AcceptChanges( ) 在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager 的 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean setModified, Boolean isCascadeDelete)
。 AcceptAllChanges(IReadOnlyList'1 changedEntries)
在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)在 Microsoft.EntityFrameworkCore.DbContext.SaveChanges(布尔型 acceptAllChangesOnSuccess)
在 C:\Projects\EFImplementationRepo\EFPersistance\Repositories\EFBaseRepository.cs:line 154 中的 EFPersistence.Repositories.EFBaseRepository'4.SaveChanges()
在 C:\Projects\EFImplementationRepo\EFImplementationRepo\Program.cs:line 31 中的 EFImplementationRepo.Program.SeedData(EFBaseRepository'4 baseRepository)
在 C:\Projects\EFImplementationRepo\EFImplementationRepo\Program.cs:line 16 中的 EFImplementationRepo.Program.Main()