如果从 SQL SERVER 2014 数据库中获取位数据类型,Dapper 总是返回 false
Dapper always returning false in case of fetching bit datatype from SQL SERVER 2014 database
我正在为基于 .NETFramework = 4.7.2 的 asp.net web api2 应用程序使用 Dapper version="2.0.78"。
这是我的 POCO class :
public class TestGroupResult : APIResult
{
public TestGroupVM TestGroup
{
get;
set;
}
}
public class TestGroupVM
{
public int TestGroupId
{
get;
set;
}
public string GroupName
{
get;
set;
}
public bool IsTestPublishedAndOnboarded
{
get;
set;
}
public bool IsPublished
{
get;
set;
}
public CountryVM Country
{
get;
set;
}
}
public async Task<TestGroupResult> GetDataAsync(RequestDTO value, string locale)
{
var localeLangId = _commonService.GetLanguageFromLocale(locale).LanguageId;
var result = new TestGroupVM();
var cgresult = new TestGroupResult();
using (var conn = await _dapperService.CreateConnection())
{
var data = await conn.QueryAsync<TestGroupVM, CountryVM, TestGroupVM>("stp_FetchData", (cg, c) =>
{
cg.Country = c;
return cg;
}, splitOn: "Uuid", param: new
{
@pI_NCOUNTRYCODE = value.Code, @pI_ILOCALELANGID = localeLangId
}
, commandType: CommandType.StoredProcedure);
result = data.FirstOrDefault();
var countryVM = new CountryVM{Uuid = Convert.ToString(result.Country.Uuid), DisplayName = result.Country.DisplayName, DisplayNameShort = result.Country.DisplayNameShort, Name = Helper.ReplaceChars(result.Country.DisplayName)};
result.Country = countryVM;
cgresult.CountryGroup = result;
}
return cgresult;
}
stp_FetchData:
SELECT CGC.TestGroupId, CGC.GroupName, C.CountryId AS Uuid, CC.DisplayName,CC.DisplayNameShort,case when C.IsPublished = 1 then 1 else 0 end AS IsTestPublishedAndOnboarded, C.IsPublished As IsPublished
FROM [Countries] C join [CountryContents] CC ON CC.CountryId = C.CountryId
join [CountryGroupAssociatedCountries] CGAC on C.CountryId = CGAC.CountryId
join [CountryGroupContents] CGC on CGC.CountryGroupId = CGAC.CountryGroupId where C.CountryCode=@pI_NCOUNTRYCODE AND CC.LanguageId IN (@pI_ILOCALELANGID)
在测试上述存储过程时,我看到以下结果:
TestGroupId : 5
GroupName: ABC
Uuid : 12
DisplayName : Test
DisplayNameShort = Testing
IsTestPublishedAndOnboarded : 1
IsPublished : 1
但是方法:GetDataAsync 总是returns
TestGroupId : 5
GroupName: ABC
Uuid : 12
DisplayName : Test
DisplayNameShort = Testing
IsTestPublishedAndOnboarded : false
IsPublished : false
我不确定为什么在这种情况下,SQL 服务器中的列类型:位未映射到 C# 中的 bool 数据类型。
任何人都可以通过提供解决此问题的指导来帮助我吗?
这里的问题出在列排序上,组件是这里的类型结构:
conn.QueryAsync<TestGroupVM, CountryVM, TestGroupVM>(...)
并注意 splitOn: "Uuid"
。
精巧的参数是:<Type1, Type2, ReturnType>
。在查询中,您的列按顺序排列:
CGC.TestGroupId
(类型 1:TestGroupVM
)
CGC.GroupName
(类型 1:TestGroupVM
)
C.CountryId AS Uuid
(类型 2:CountryVM
- 注意:此时,我们切换到填充 CountryVM
对象,因为这就是splitOn
说到做到)
CC.DisplayName
(类型 2:CountryVM
)
CC.DisplayNameShort
(类型 2:CountryVM
)
case when C.IsPublished = 1 then 1 else 0 end AS IsTestPublishedAndOnboarded
(类型 2:CountryVM
- 不是 属性!)
C.IsPublished As IsPublished
(类型 2:CountryVM
- 不是 属性!)
所以总的来说,我们在移动到第二个对象后尝试在 CountryVM
上填充属性......但它没有这些属性。
要解决此问题,您要做的是将那两列移动到 Uuid
开关之前,以便它映射到正确的位置,例如:
SELECT CGC.TestGroupId, CGC.GroupName, case when C.IsPublished = 1 then 1 else 0 end AS IsTestPublishedAndOnboarded, C.IsPublished As IsPublished, C.CountryId AS Uuid, CC.DisplayName,CC.DisplayNameShort
...或者,将这些属性移动到它们所属的国家/地区(我注意到此处与您的数据库模型不匹配 - 因此请考虑该选项!)
我正在为基于 .NETFramework = 4.7.2 的 asp.net web api2 应用程序使用 Dapper version="2.0.78"。
这是我的 POCO class :
public class TestGroupResult : APIResult
{
public TestGroupVM TestGroup
{
get;
set;
}
}
public class TestGroupVM
{
public int TestGroupId
{
get;
set;
}
public string GroupName
{
get;
set;
}
public bool IsTestPublishedAndOnboarded
{
get;
set;
}
public bool IsPublished
{
get;
set;
}
public CountryVM Country
{
get;
set;
}
}
public async Task<TestGroupResult> GetDataAsync(RequestDTO value, string locale)
{
var localeLangId = _commonService.GetLanguageFromLocale(locale).LanguageId;
var result = new TestGroupVM();
var cgresult = new TestGroupResult();
using (var conn = await _dapperService.CreateConnection())
{
var data = await conn.QueryAsync<TestGroupVM, CountryVM, TestGroupVM>("stp_FetchData", (cg, c) =>
{
cg.Country = c;
return cg;
}, splitOn: "Uuid", param: new
{
@pI_NCOUNTRYCODE = value.Code, @pI_ILOCALELANGID = localeLangId
}
, commandType: CommandType.StoredProcedure);
result = data.FirstOrDefault();
var countryVM = new CountryVM{Uuid = Convert.ToString(result.Country.Uuid), DisplayName = result.Country.DisplayName, DisplayNameShort = result.Country.DisplayNameShort, Name = Helper.ReplaceChars(result.Country.DisplayName)};
result.Country = countryVM;
cgresult.CountryGroup = result;
}
return cgresult;
}
stp_FetchData:
SELECT CGC.TestGroupId, CGC.GroupName, C.CountryId AS Uuid, CC.DisplayName,CC.DisplayNameShort,case when C.IsPublished = 1 then 1 else 0 end AS IsTestPublishedAndOnboarded, C.IsPublished As IsPublished
FROM [Countries] C join [CountryContents] CC ON CC.CountryId = C.CountryId
join [CountryGroupAssociatedCountries] CGAC on C.CountryId = CGAC.CountryId
join [CountryGroupContents] CGC on CGC.CountryGroupId = CGAC.CountryGroupId where C.CountryCode=@pI_NCOUNTRYCODE AND CC.LanguageId IN (@pI_ILOCALELANGID)
在测试上述存储过程时,我看到以下结果:
TestGroupId : 5
GroupName: ABC
Uuid : 12
DisplayName : Test
DisplayNameShort = Testing
IsTestPublishedAndOnboarded : 1
IsPublished : 1
但是方法:GetDataAsync 总是returns
TestGroupId : 5
GroupName: ABC
Uuid : 12
DisplayName : Test
DisplayNameShort = Testing
IsTestPublishedAndOnboarded : false
IsPublished : false
我不确定为什么在这种情况下,SQL 服务器中的列类型:位未映射到 C# 中的 bool 数据类型。
任何人都可以通过提供解决此问题的指导来帮助我吗?
这里的问题出在列排序上,组件是这里的类型结构:
conn.QueryAsync<TestGroupVM, CountryVM, TestGroupVM>(...)
并注意 splitOn: "Uuid"
。
精巧的参数是:<Type1, Type2, ReturnType>
。在查询中,您的列按顺序排列:
CGC.TestGroupId
(类型 1:TestGroupVM
)CGC.GroupName
(类型 1:TestGroupVM
)C.CountryId AS Uuid
(类型 2:CountryVM
- 注意:此时,我们切换到填充CountryVM
对象,因为这就是splitOn
说到做到)CC.DisplayName
(类型 2:CountryVM
)CC.DisplayNameShort
(类型 2:CountryVM
)case when C.IsPublished = 1 then 1 else 0 end AS IsTestPublishedAndOnboarded
(类型 2:CountryVM
- 不是 属性!)C.IsPublished As IsPublished
(类型 2:CountryVM
- 不是 属性!)
所以总的来说,我们在移动到第二个对象后尝试在 CountryVM
上填充属性......但它没有这些属性。
要解决此问题,您要做的是将那两列移动到 Uuid
开关之前,以便它映射到正确的位置,例如:
SELECT CGC.TestGroupId, CGC.GroupName, case when C.IsPublished = 1 then 1 else 0 end AS IsTestPublishedAndOnboarded, C.IsPublished As IsPublished, C.CountryId AS Uuid, CC.DisplayName,CC.DisplayNameShort
...或者,将这些属性移动到它们所属的国家/地区(我注意到此处与您的数据库模型不匹配 - 因此请考虑该选项!)