简体   繁体   中英

Is there a way to set a default value (for example based on IOptions) to ignored property when configuring entities?

I have a property on my entity, that does not exist in the database, so during entity configuration, I ignore it:

builder.Ignore(m => m.MaxDeliveryAttempts);

However I need this property to be by default set to a value I get from my appsettings.json file.

I can get IOptions injected into by DbContext, but I cannot figure out how to force EF to set the value of MaxDeliveryAttempts from settings when it instantiates the entity.

Is it even possible?

You can configure the default value. For example:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>()
        .Property(b => b.Rating)
        .HasDefaultValue(3);
}

Reference: Default values

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