dotnet/EntityFramework.Docs

Document that IEntityTypeConfiguration.Configure() is not fired for entities without default constructor

Chiusa

#3207 aperta il 21 mar 2021

 (11 commenti) (0 reazioni) (0 assegnatari)PowerShell (1961 fork)batch import
area-model-buildinggood first issue

Metriche repository

Star
 (1625 stelle)
Metriche merge PR
 (Merge medio 1g 6h) (30 PR mergiate in 30 g)

Descrizione

File a bug

I looked through open and closed issues and haven't found any similar so here is my report:

For simple enity

    public class User: IEntityTypeConfiguration<User>
    {
        public long Id { get; set; }

        public string Name { get; set; } = string.Empty;
        
        public void Configure(EntityTypeBuilder<User> user)
        {
            user.HasIndex(x => x.Name);
        }
    }

when I use IEntityTypeConfiguration to reverse DbContext configuration from context OnModelCreating() into entity itself it works fine generating migration having migrationBuilder.CreateIndex()

        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "user",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    name = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_user", x => x.id);
                });

            **migrationBuilder.CreateIndex(
                name: "ix_user_name",
                table: "user",
                column: "name");**
        }

If I want to have custom constructor like this

    public class User : IEntityTypeConfiguration<User>
    {
        public User(string name)
        {
            Name = name;
        }

        public long Id { get; set; }

        public string Name { get; set; } = string.Empty;

        public void Configure(EntityTypeBuilder<User> user)
        {
            user.HasIndex(x => x.Name);
        }
    }

it generates empty migration unless public DbSet<User> Users { get; set; } = null!; is included into DbContext explicitly. Migration generated then is as follows:

protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "users",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    name = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_users", x => x.id);
                });
        }

which is obvious proof that IEntityTypeConfiguration.Configure() is not fired during configuration.

Expected behavior is to have the same migration no matter what constructors entity has cause DB model does not depend on them.

Ofcourse I can add default constructor explicitly, but how can I have custom one only e.g. for the purpose to have insuranse Name property is set ?

Include provider and version information

EF Core version: 5.0.2 Database provider: Npgsql.EntityFrameworkCore.PostgreSQL Target framework: .NET 5.0) Operating system: Windows 10 IDE: Visual Studio 2019 16.8.6

Guida contributor