[英]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.但是,我需要将此属性默认设置为我从 appsettings.json 文件中获得的值。
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.我可以让
IOptions
注入 IOptions,但我无法弄清楚如何强制 EF 在实例化实体时从设置中设置MaxDeliveryAttempts
的值。
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参考: 默认值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.