dotnet/EntityFramework.Docs

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

已关闭

#3,207 创建于 2021年3月21日

 (11 条评论) (0 个反应) (0 位负责人)PowerShell (1,961 个派生)batch import
area-model-buildinggood first issue

仓库指标

星标
 (1,625 个星标)
PR 合并指标
 (平均合并 1天 6小时) (30 天内合并 30 个 PR)

描述

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

贡献者指南