dotnet / dotnet/efcore

Validate that foreign key field matches referenced field during migration generation (not just application)

Open
#36,875 4 comments 0 reactions 1 assignee Claimed by @AndriySvyryd View on GitHub
area-model-building customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### What problem are you trying to solve?

I received the following error APPLYING a migration:

```
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (31ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [BlockDefinitions] ADD CONSTRAINT [FK_BlockDefinitions_Designs_DesignName] FOREIGN KEY ([DesignName]) REFERENCES [Designs] ([Name]) ON DELETE CASCADE;
fail: Microsoft.Extensions.Hosting.Internal.Host[9]
BackgroundService failed
Microsoft.Data.SqlClient.SqlException (0x80131904): Column 'Designs.Name' is not the same length or scale as referencing column 'BlockDefinitions.DesignName' in foreign key 'FK_BlockDefinitions_Designs_DesignName'. Columns participating in a foreign key relationship must be defined with the same length and scale.
Could not create constraint or index. See previous errors.
```

as I failed to apply a MaxLength attribute to a varchar typed primary key.

### Describe the solution you'd like

Please validate this condition (with at least a warning), when ADDING a migration, instead of (or in addition to) when applying migration.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.