3

Using prerelease of EF Core (3.0.0-preview6.19304.10). When I call DbContext.SaveChanges(), it causes an error

Value cannot be null. Parameter name frameworkName

Here is the context class

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());
        }
    }
}

Models' configurations:

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);
    }
}

And the entities:

        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();
        }

Here is error stack trace

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

4
  • where is your entity and maps cs file? Commented Jul 17, 2019 at 13:14
  • Added to the question Commented Jul 17, 2019 at 13:19
  • but ı can not see entities? Commented Jul 17, 2019 at 13:41
  • 1
    How to fix? Prerelease (beta) software is expected to have problems and not functioning properly. The solution is simple - don't use prerelease software, switch to the latest stable EF Core version. Commented Jul 17, 2019 at 13:51

3 Answers 3

9

We updated this week our application to EF Core 3.1 and faced an issue with the same error message. The reason was, that we had collection navigation properties without setters. There is already an issue on GitHub.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I had the same problem for the same reason while downgrading from 3.1 to 2.2.
0

Just wanted to put this here in case someone else was having this problem.

This was happening to me as well for a .Net Framework web application, it was really weird because it only happened to one of our clients, the rest were fine. What I ended up finding is that the one that wasn't working didn't have the http runtime target framework set so it was running under .net 4.0, causing it to throw that exception everytime ef core was trying to initialise a collection that was set to readonly.

So the fix was changing the http runtime to the one we were using (4.7.2) and this got sorted.

<httpRuntime requestValidationMode="2.0" targetFramework="4.7.2" executionTimeout="600" />

Comments

-4

Currently, DbContext.SaveChanges() is working, exception is thrown after saving data. Such way is working for me:

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

2 Comments

Just to explain why this is very bad for anyone who might not realize it, this simply lets the command fail, catches the error and silently ignores it. So, the application not only no longer works, it also doesn't notify you that something went wrong.
We should never catch an exception and do nothing with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.