![](/img/trans.png)
[英]Is it possible to assign set the default value of a class property based on another property?
[英]Is there a way to set a default value (for example based on IOptions) to ignored property when configuring entities?
我的實體上有一個屬性,該屬性在數據庫中不存在,因此在實體配置期間,我忽略它:
builder.Ignore(m => m.MaxDeliveryAttempts);
但是,我需要將此屬性默認設置為我從 appsettings.json 文件中獲得的值。
我可以讓IOptions
注入 IOptions,但我無法弄清楚如何強制 EF 在實例化實體時從設置中設置MaxDeliveryAttempts
的值。
甚至可能嗎?
您可以配置默認值。 例如:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.Rating)
.HasDefaultValue(3);
}
參考: 默認值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.