Closed stevenxi closed 2 weeks ago
@stevenxi EF model building always configures the PK property as required, even if the CLR type is nullable. Migrations creates a database schema that most closely matches this. If you want the PK to be nullable in the database, then you can edit that in the database, but since EF doesn't support null primary key values, it will make the database incompatible with EF if there are any nulls in that column.
@ajcvickers , I understand that, after all, not all database supports nullable PK, SQL Server is just an execption. So it's absolutely fine.
But user need to be notified during migration generation, otherwise there will be runtime error. Just like 'lazy loading property not being virtual' kind of exception, during either migration, or EF initialization.
Include your code
Migration generated as following:
Migration script generated as following:
It should be either allows NULL PK, or throws exception when generating migrations. But not changed mapping quietly.
Include verbose output
Please include
--verbose
output when filing bugs about thedotnet ef
or Package Manager Console tools.Use triple-tick fences for tool output. For example:
Include provider and version information
EF Core version: 8.0.7 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: net8 Operating system: Windows 11 IDE: Visual Studio 2022 17.10.3