-
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
EF Core 6.0 InvalidOperationException: The object has been removed from the model #26611
Labels
area-model-building
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
Servicing-approved
type-bug
Milestone
Comments
Full repro: public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.ToTable("orders").HasKey(r => r.Id);
// other property config etc
// Charge Items
builder.HasMany(p => p.ChargeItems)
.WithOne()
.HasForeignKey("order_id")
.HasConstraintName("charge_items_order_id_fk")
.IsRequired()
.OnDelete(DeleteBehavior.Cascade)
.Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field);
}
}
public class Order
{
public int Id { get; set; }
public ICollection<ChargeItem> ChargeItems { get; set; }
}
public class ChargeItem
{
public int Id { get; set; }
}
public class SomeDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(Your.ConnectionString)
.LogTo(Console.WriteLine, LogLevel.Debug)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new OrderConfiguration());
}
}
public class Program
{
public static void Main()
{
using (var context = new SomeDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}
}
} |
AndriySvyryd
added a commit
that referenced
this issue
Nov 17, 2021
AndriySvyryd
added
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
Servicing-consider
labels
Nov 17, 2021
AndriySvyryd
added a commit
that referenced
this issue
Nov 17, 2021
As a workaround you can separate builder.HasMany(p => p.ChargeItems)
.WithOne()
.HasForeignKey("order_id")
.HasConstraintName("charge_items_order_id_fk")
.IsRequired()
.OnDelete(DeleteBehavior.Cascade)
.Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field); into builder.HasMany(p => p.ChargeItems)
.WithOne()
.HasForeignKey("order_id")
.HasConstraintName("charge_items_order_id_fk")
.IsRequired(); and builder.HasMany(p => p.ChargeItems)
.WithOne()
.OnDelete(DeleteBehavior.Cascade)
.Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field); |
FYI for those impacted by this issue: EF Core 6.0.1 is now available from NuGet. |
And the exception doesn't tell you which object it doesn't like.. (v 6.0.7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
area-model-building
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
Servicing-approved
type-bug
Am in the process of upgrading a WebApi from .net5 to .net6. Am hitting a wall with the following exception related to EF Core 6.0 during the Entity Configuration of a domain entity called "Order" which has a one-to-many relationship to "ChargeItems":
This is the offending code:
Which is causing the following exception:
I posted this on Stackoverflow and the suggestion was that there was a bug related to
The full exception stack trace is as follows:
The text was updated successfully, but these errors were encountered: