简体   繁体   中英

Issue with base class in EF Core 2.2

I have many classes looks like following:

public class ModelOneConfiguration
{
    public ModelOneConfiguration(EntityTypeBuilder<ModelOne> entity)
    {
        entity.Property(e => e.Id).IsRequired().HasDefaultValueSql("NEWID()");
        entity.Property(e => e.CreatedAt).HasDefaultValueSql("getutcdate()").ValueGeneratedOnAdd();
        entity.Property(e => e.UpdatedAt).HasDefaultValueSql("getutcdate()").ValueGeneratedOnUpdate();
        entity.Property(e => e.FirstName).IsRequired();
        entity.Property(e => e.LastName).IsRequired();         
        entity.Property(e => e.Email).IsRequired();
    }

}

And call it from Db context class:

protected override void OnModelCreating(ModelBuilder builder)
{
    new ModelOneConfiguration(builder.Entity<ModelOne>());
    ….other classes as well…
    base.OnModelCreating(builder);       
}

When I migrate it works fine with following results:

Id = table.Column<Guid>(nullable: false, defaultValueSql: "NEWID()"),
CreatedAt = table.Column<DateTimeOffset>(nullable: false, defaultValueSql: "getutcdate()"),
UpdatedAt = table.Column<DateTimeOffset>(nullable: true, defaultValueSql: "getutcdate()"),
FirstName = table.Column<string>(nullable: false),
LastName = table.Column<string>(nullable: false),
Email = table.Column<string>(nullable: false)

So far so Good.

Now I tried to wrap up Id, CreatedAt and UpdatedAt in base class so I can use it with all my other model configuration classes. So what I did, I was following this example answered by Ivan Stoev.

So for my base class I did following

public class BaseConfiguration<TEntity> : IEntityTypeConfiguration<TEntity> where TEntity : BaseEntities
{
    public virtual void Configure(EntityTypeBuilder<TEntity> entity)
    {
        entity.Property(e => e.Id).IsRequired().HasDefaultValueSql("NEWID()");
        entity.Property(e => e.CreatedAt).HasDefaultValueSql("getutcdate()").ValueGeneratedOnAdd();
        entity.Property(e => e.UpdatedAt).HasDefaultValueSql("getutcdate()").ValueGeneratedOnUpdate();
    }
}

Then I extended my model configure class:

public class ModelOneConfiguration : BaseConfiguration<ModelOne>
{
    public ModelOneConfiguration(EntityTypeBuilder<ModelOne> entity)
    {
        entity.Property(e => e.FirstName).IsRequired();
        entity.Property(e => e.LastName).IsRequired();
        entity.Property(e => e.Email).IsRequired();
    }
}

Now when I run migration It generate following

Id = table.Column<Guid>(nullable: false),
CreatedAt = table.Column<DateTimeOffset>(nullable: false),
UpdatedAt = table.Column<DateTimeOffset>(nullable: true),
FirstName = table.Column<string>(nullable: false),
LastName = table.Column<string>(nullable: false),
Email = table.Column<string>(nullable: false)

As we can see, defaultValueSql: "NEWID()" , defaultValueSql: "getutcdate()" are just missing in Id , CreatedAt and UpdatedAt .

And I am using Core 2.2 with EF core 2.2.

My Question is very simple:

What I am doing wrong and how can this be fixed?

Your derived class should be doing the work inside the Configure method, and not in the constructor. Add an override for that, and make sure to call the base Configure method:

public class ModelOneConfiguration : BaseConfiguration<ModelOne>
{
    public override void Configure(EntityTypeBuilder<ModelOne> builder)
    {
        base.Configure(builder);

        entity.Property(e => e.FirstName).IsRequired();
        entity.Property(e => e.LastName).IsRequired();
        entity.Property(e => e.Email).IsRequired();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM