Azure 存储中的默认值

Default Value in Azure Storage

我正在我的 Azure 模型中定义一个 属性,我想要它的默认值。我正在做的是这个

[DefaultValue(true)]
public Boolean SmsNotification { get; set; }

默认值仍然是 False。谁能告诉我如何解决这个问题?

您可以创建一个 属性 并将包装字段设置为 true:

public bool mSmsNotification = true;
public bool SmsNotification
{
    get { return mSmsNotification; }
    set { mSmsNotification = value; }
}

DefaultValueAttribute 就是:一个属性。文档很清楚:

A visual designer can use the default value to reset the member's value. Code generators can use the default values also to determine whether code should be generated for the member.

A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.

不知道能不能说的更清楚一点

DefaultValue attribute does not cause the initial value to be initialized with 属性的 value.It 并没有在代码中设置属性的实际默认值,它只是为了设计.

所以 :

[DefaultValue(true)]
public bool SmsNotification
{
     get { return _smsNotification; }
     set { _smsNotification = value; }
}

For details see this old post.