Reputation: 677
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?
Upvotes: 2
Views: 254
Reputation: 119136
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();
}
}
Upvotes: 3