cascading foreign key 'X' cannot be created where the referencing column 'Y' is an identity column

ef-core-3.0 entity-framework-core

Question

I'm trying to upgrade the OwnedTypes sample to EF Core 3.0 Preview 8 but when I run the project it can't create the database.

When Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated() is called the following exception is thrown:

Microsoft.Data.SqlClient.SqlException: 'Cascading foreign key 'FK_OrderDetails_DetailedOrders_OrderId' cannot be created where the referencing column 'OrderDetails.OrderId' is an identity column. Could not create constraint or index. See previous errors.'

What previous errors? 🤔

protected override void OnModelCreating(ModelBuilder modelBuilder)
{            
    #region OwnsOneNested
    modelBuilder.Entity<DetailedOrder>().OwnsOne(p => p.OrderDetails, od =>
    {
        od.OwnsOne(c => c.BillingAddress);
        od.OwnsOne(c => c.ShippingAddress);
    });
    #endregion

    #region OwnsOneTable
    modelBuilder.Entity<DetailedOrder>().OwnsOne(p => p.OrderDetails, od =>
    {
        od.OwnsOne(c => c.BillingAddress);
        od.OwnsOne(c => c.ShippingAddress, sa =>
        {
            sa.Ignore(p => p.IgnoreMe);
        });
        od.ToTable("OrderDetails");
    });
    #endregion
}

Full project

I assume there is something in the 3.0 breaking changes list I need to follow?

1
0
8/21/2019 6:59:14 PM

Accepted Answer

  • You can remove od.ToTable("OrderDetails"); to map OrderDetails to the same table as DetailedOrder
  • You can add od.Property("OrderId").ValueGeneratedNever(); to disable marking the OrderId column as identity
0
8/27/2019 6:10:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow