You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
I have a primary key as a foreign key in Entity Framework.
This seems to work fine, and generates the following table.
But I get an error when I try to change this PK/FK so that it references a different record.
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
The text was updated successfully, but these errors were encountered: