Dapper不反序列化nvarchar类型

时间:2019-03-29 18:19:19

标签: c# dapper

基本上,我有一个播放列表对象:

public class Playlist
{
    public int PlaylistID { get; set; }
    public string PlaylistName { get; set; }
    public string PlaylistUri { get; set; }
    public int FestivalID { get; set; }
}

当我尝试从数据库中读取数据时,将填充PlaylistID和FestivalID数据,但不会填充PlaylistName / PlaylistUri。我已经检查了数据类型:

COLUMN_NAME DATA_TYPE
PlaylistID  int
PlaylistName nvarchar
PlaylistUri nvarchar
FestivalID  int

这也是获取数据的代码(非常多的alpha,准备就绪后会清除)

public class SpotifyApiData
{
    private string _connectionString;
    private string ALL_DATA = "SELECT * FROM EdgeCases; SELECT [PlaylistID] ,[PlaylistName],[PlaylistID] FROM Playlist; SELECT * FROM Festival;";
    public string ConnectionString { get => _connectionString; set => _connectionString = value; }


    public SpotifyApiData()
    {
        _connectionString = ConfigurationHelper.GetConnectionString();
    }

    public void GetRefreshablePlaylist()
    {

    }

    public PlaylistCollectionData GetAllData()
    {
        using (var connection = new SqlConnection(ConnectionString))
        {
            using (var multi = connection.QueryMultiple(ALL_DATA))
            {

                return new PlaylistCollectionData()
                {
                    EdgeCases = multi.Read<EdgeCases>().ToList(),
                    Festivals = multi.Read<Festival>().ToList(),
                    Playlists = multi.Read<Playlist>().ToList()
                };
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您正在PlayList之前阅读Festival。反转这两行以匹配选择查询的顺序

return new PlaylistCollectionData()
{
    EdgeCases = multi.Read<EdgeCases>().ToList(),
    Playlists = multi.Read<Playlist>().ToList(),
    Festivals = multi.Read<Festival>().ToList()
};

此外,播放列表的选择似乎不完整,应该是

 SELECT [PlaylistID],[PlaylistName],[PlaylistUri],[FestivalID] FROM Playlist;