繁体   English   中英

是否可以通过用于转换值的属性的设置器来设置属性?

[英]Is it possible to set a property via the setter of a property used to convert a value?

我们将 EFCore 与 Azure SQL 数据库一起使用。 其中一列Yodels是一个 tinyint,它只能映射到 C# 中的一个字节,但我们希望将它与 web 表单上的复选框一起使用,该表单需要一个布尔值。 为了显示它,我们通过附加属性YodelsBool访问它,该属性将其转换为布尔值。

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public byte Yodels { get; set; }

    public bool YodelsBool
    {
        get {
            return Yodels > 0;
        }
    }
}

表单上的复选框: <input asp-for="Person.YodelsBool" class="form-control" type="checkbox" value="Person.YodelsBool" />

我们还希望能够通过这个 YodelsBool 属性设置值。 那可能吗? 当我们添加一个 setter 时,它会失败Invalid column name 'YodelsBool' ,因为它正在尝试更新该特定属性的数据库。

我们尝试了一个二传手:

    public bool YodelsBool
    {
        get {
            return Yodels > 0;
        }
        set {
                Yodels = Covert.ToByte(value);
        }
    }

我们如何在不更改数据库的情况下完成这项工作? 我们正在使用 tinyint 以防我们希望在未来某个时候扩展到 true/false 之外。

[NotMapped]添加到属性:

[NotMapped]
public bool YodelsBool
{
    get {
        return Yodels > 0;
    }
    set {
            Yodels = Covert.ToByte(value);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM