dotnet / dotnet/efcore

Column treated as nullable if its name is <Entity>Id

Open
#34,928 7 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

If I create an initial migration for the below context with 8.0.10, EntityId is created as nullable, even though it is marked as required, and the index is created with a NULL filter. If I rename it to `XEntityId`, it works as expected.

```C#
using System;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

await using var context = new MyContext();
await context.Database.MigrateAsync();

public class MyContext : DbContext
{
public DbSet Entities { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.LogTo(Console.WriteLine)
.UseSqlServer("Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=Migrations;Integrated Security=SSPI");
}

[Index(nameof(EntityId), IsUnique = true)]
public class Entity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Key { get; set; }

[Required]
[MaxLength(50)]
public required string EntityId { get; set; }
}
```

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.