返回空字符串而不是 Null

chobo2

我正在使用 ef core 和mapster我的数据库中有一些可以为空的列。

当我从数据库中获取它们时,C# 将它们存储为空值(这是有道理的)。我想返回这些字段是空字符串,但当我通过我的 api 发送它们时。

public class CompanyDto
    {

        public string Website { get; set; }
    }


 public class Company
    {
        public int Id { get; set; }
        public string Website { get; set; } = "";
    }

company.Adapt<CompanyDto>()

使 CompanyDto 中的网站成为空字符串的最佳方法是什么。

法比奥

经典二传手也能胜任

public class Company
{
    public int Id { get; set; }

    private string _website;
    public string Website 
    { 
        get { return _website; }
        set { _website = value ?? string.Empty; }
    };

    public Company ()
    {
        _website = string.empty;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章