0

我正在使用 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 中的网站是一个空字符串。

4

1 回答 1

0

经典二传手也能胜任

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;
    }
}
于 2018-07-08T19:03:20.050 回答