You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MSSQL bit类型转C# bool类型时取值异常问题排查

MSSQL bit类型与C# bool交互返回值全为false的问题

数据库表dbo.UserRoles包含多个bit类型字段,存储true/false值。API查询后返回的所有bool字段值均为false,但数据库中实际是true/false交替的。

相关代码

控制器代码

public async Task<ActionResult> GetUserRoles([FromBody] UserRolesID userID)
{
    string UserID = userID.UserID.ToString();
    UserRoles userRoles = _apiKeyService.GetUserRoles(UserID);
    if(userRoles != null)
    {
        var ReturnString = JsonConvert.SerializeObject(userRoles);
        return Ok(ReturnString);
    }
    else
    {
        return BadRequest("No roles found");
    }
}

服务层查询代码

public UserRoles GetUserRoles(string userID)
{
    var query = (from u in _dbContext.UserRoles
                 where u.UserID == userID
                 select new UserRoles());
    return query.First();
}

UserRoles实体类

public class UserRoles
{
    public string UserID { get; set; } = "";
    public bool Appointment_View { get; set; }
    public bool Appointment_Modify { get; set; }
    public bool Foster_View { get; set; }
    public bool Foster_Modify { get; set; }
    public bool Dog_View { get; set; }
    public bool Dog_Modify { get; set; }
    public bool Rules_View { get; set; }
    public bool Rules_Modify { get; set; }
    public bool User_Approve { get; set; }
    public bool User_Modify { get; set; }
}

API响应结果

{"UserID":"","Appointment_View":false,"Appointment_Modify":false,"Foster_View":false,"Foster_Modify":false,"Dog_View":false,"Dog_Modify":false,"Rules_View":false,"Rules_Modify":false,"User_Approve":false,"User_Modify":false}

问题原因

服务层查询语句中select new UserRoles()是创建了一个全新的空实体对象,并没有将数据库中查询到的字段值赋值给这个新对象。C#中bool类型默认值为false,string类型默认值为空字符串,所以返回的结果全是默认值。

解决方案

1. 直接返回数据库实体对象

修改服务层代码,直接选择数据库中的实体实例,无需手动创建新对象:

public UserRoles GetUserRoles(string userID)
{
    return _dbContext.UserRoles.FirstOrDefault(u => u.UserID == userID);
}

或使用LINQ查询语法:

public UserRoles GetUserRoles(string userID)
{
    var query = from u in _dbContext.UserRoles
                where u.UserID == userID
                select u; // 直接选择数据库中的实体u
    return query.FirstOrDefault();
}

使用FirstOrDefault替代First更安全,当无匹配数据时会返回null,避免抛出异常。

2. 手动构造对象时明确赋值属性

如果需要自定义返回字段(比如只返回部分字段),必须手动将数据库字段值赋值给新对象的对应属性:

public UserRoles GetUserRoles(string userID)
{
    var query = from u in _dbContext.UserRoles
                where u.UserID == userID
                select new UserRoles
                {
                    UserID = u.UserID,
                    Appointment_View = u.Appointment_View,
                    Appointment_Modify = u.Appointment_Modify,
                    Foster_View = u.Foster_View,
                    Foster_Modify = u.Foster_Modify,
                    Dog_View = u.Dog_View,
                    Dog_Modify = u.Dog_Modify,
                    Rules_View = u.Rules_View,
                    Rules_Modify = u.Rules_Modify,
                    User_Approve = u.User_Approve,
                    User_Modify = u.User_Modify
                };
    return query.FirstOrDefault();
}

3. 验证实体映射配置

EF/EF Core默认会将MSSQL的bit类型映射到C#的bool类型,无需额外配置。如果有自定义映射,检查配置是否正确:

// 在DbContext的OnModelCreating方法中配置示例
modelBuilder.Entity<UserRoles>()
    .Property(u => u.Appointment_View)
    .HasColumnType("bit");

内容的提问来源于stack exchange,提问作者tlf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 11:33:21