-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
InvalidOperationException on save after upgrading from 5 to 6 #27455
Comments
@csmager I can't reproduce this--see my code below. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. @AndriySvyryd With the code below I get the following model and change tracker state. The update pipeline then attempts to insert null. Any idea what is happening here?
#nullable enable
public class Foo
{
public int Id { get; set; }
public int? CurrentPeriodNumber { get; set; }
public FooPeriod? CurrentPeriod { get; set; }
public ICollection<FooPeriod> Periods { get; } = new HashSet<FooPeriod>();
}
public class FooPeriod
{
public int FooId { get; set; }
public int PeriodNumber { get; set; }
public Foo? Foo { get; set; }
}
public class SomeDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(Your.ConnectionString)
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Foo>(entity =>
{
entity.HasKey(x => x.Id);
entity.Property(x => x.Id)
.ValueGeneratedOnAdd();
entity.HasOne(x => x.CurrentPeriod)
.WithOne()
.HasForeignKey<Foo>(x => new { x.Id, x.CurrentPeriodNumber });
});
modelBuilder.Entity<FooPeriod>(entity =>
{
entity.HasKey(x => new { x.FooId, x.PeriodNumber });
entity.HasOne(x => x.Foo)
.WithMany(x => x.Periods)
.HasForeignKey(x => x.FooId);
});
}
}
public class Program
{
public static void Main()
{
using (var context = new SomeDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var period = new FooPeriod
{
PeriodNumber = 1,
Foo = new Foo()
};
context.Add(period);
context.SaveChanges();
}
}
}
|
Hi @ajcvickers - apologies, the above generates the exception I pasted when I use SQLite. If I swap to SQL Server I do get the same error you've pasted above - in my brief test before logging I hadn't noticed the exception was different. To get SQL Server to generate the error I referred to, the configuration for entity.Property(x => x.Id)
.ValueGeneratedOnAdd()
.UseIdentityColumn(); |
Don't throw for unknown values that won't be sent to the database Fixes #27455
On attempting to upgrade our project from EF Core 5 to 6, most of our integration tests fail. I've narrowed this down to a small repro below. An entity
Foo
has manyFooPeriods
. One of these is 'current'. So we have a 1:many and a 1:1 relationship:This is configured via the fluent builder as below:
If I insert a new period as below:
Then I get this exception:
I've dug around to see what might have changed between 5 and 6 and am pretty confident the change in behaviour was caused by the fix for #22603. I also note that this is the same sort of model as #26629 (which prevented us updating to 6.0.0).
EF Core version: 6.0.2
Database provider: I've tried Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Sqlite
Target framework: .NET 6.0
Operating system: macOS
The text was updated successfully, but these errors were encountered: