Skip to content

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

Open
@stevendarby

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.

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<Entity> 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; }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions