Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stevendarby opened this issue Oct 17, 2024 · 1 comment
Open

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

stevendarby opened this issue Oct 17, 2024 · 1 comment

Comments

@stevendarby
Copy link
Contributor

stevendarby commented Oct 17, 2024

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; }
}
@roji
Copy link
Member

roji commented Oct 17, 2024

Confirmed, this also repros on 9.0.0-rc.2:

CREATE TABLE [Entities] (
    [Key] int NOT NULL,
    [EntityId] nvarchar(50) NULL,
    CONSTRAINT [PK_Entities] PRIMARY KEY ([Key])
);

Renaming EntityId indeed makes it

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

No branches or pull requests

3 participants