Skip to content
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

Error when modifying a primary key as a foreign key #26235

Closed
SoftCircuits opened this issue Oct 2, 2021 · 1 comment
Closed

Error when modifying a primary key as a foreign key #26235

SoftCircuits opened this issue Oct 2, 2021 · 1 comment

Comments

@SoftCircuits
Copy link

SoftCircuits commented Oct 2, 2021

I have a primary key as a foreign key in Entity Framework.

public class RailcarTrip
{
    [Key, ForeignKey("WaybillRailcar")]
    public int WaybillRailcarId { get; set; }
    public WaybillRailcar WaybillRailcar { get; set; }

    // Etc.
}

This seems to work fine, and generates the following table.

CREATE TABLE [dbo].[RailcarTrips](
	[WaybillRailcarId] [int] NOT NULL,
	[StartDate] [datetime2](7) NOT NULL,
	[DeliveryDate] [datetime2](7) NULL,
	[ReleaseDate] [datetime2](7) NULL,
	[ReturnDate] [datetime2](7) NULL,
	[DeliveryEta] [datetime2](7) NULL,
	[ReleaseEta] [datetime2](7) NULL,
	[ReturnEta] [datetime2](7) NULL,
	[ReturnCity] [nvarchar](80) NULL,
	[ReturnState] [nvarchar](2) NULL,
	[TripType] [int] NOT NULL,
 CONSTRAINT [PK_RailcarTrips] PRIMARY KEY CLUSTERED 
(
	[WaybillRailcarId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[RailcarTrips]  WITH CHECK ADD  CONSTRAINT [FK_RailcarTrips_WaybillRailcars_WaybillRailcarId] FOREIGN KEY([WaybillRailcarId])
REFERENCES [dbo].[WaybillRailcars] ([Id])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[RailcarTrips] CHECK CONSTRAINT [FK_RailcarTrips_WaybillRailcars_WaybillRailcarId]
GO

But I get an error when I try to change this PK/FK so that it references a different record.

The property 'RailcarTrip.WaybillRailcarId' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key, first delete the dependent and invoke 'SaveChanges', and then associate the dependent with the new principal.

I don't understand why this is a problem? The primary key is not set as an identity/autoincrement. This code should only require a simple update of the FK to reference a different row ID. I don't want to have to delete anything. Can anyone explain why it's an issue? Why can't I change this value?

If this can't be supported, is there a more efficient workaround?

Include provider and version information

EF Core version: 5.0.10
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.11.3

@ajcvickers
Copy link
Member

@SoftCircuits This is tracked by #4073.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants