简体   繁体   English

有没有办法在配置实体时将默认值(例如基于 IOptions)设置为忽略的属性?

[英]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.

相关问题 是否可以基于另一个属性将set属性的默认值分配给set? - Is it possible to assign set the default value of a class property based on another property? 具有默认动态值的 IOptions 模式未按预期工作 - IOptions pattern with default dynamic value not working as expected 映射到 CosmosDB 集合时,有什么方法可以将默认值设置为 Entity Framework Core 实体的属性 - Is there any way to set default value to a property of Entity Framework Core entity when mapping to CosmosDB collection 为属性设置默认值 - Set a default value to a property Ef Core 2将值设置为运行时被忽略的属性 - Ef Core 2 set value to ignored property on runtime 有没有一种优雅的方法来为c#中的属性设置默认值? - Is there an elegant way to set a default value for a property in c#? 有没有办法知道 object 属性值是否已设置或是否为默认值? - Is there a way to know if object property value is already set or is it default? 在自定义属性上设置默认值 - Set default value on a custom property EF-CF-Set另一个表值中的“ ignored”属性 - EF-CF-Set 'ignored' property from another table value 将C#WinForm Stop控件属性设置为默认值时将其设置为默认值 - C# WinForm Stop control property setting to default when it is set to be a value which is also the default
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM