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

EntityFramework查询触发SqlNullValueException,插入操作后查询恢复正常的问题求助

EntityFramework查询触发SqlNullValueException,插入操作后查询恢复正常的问题求助

各位大佬,我遇到一个特别诡异的EntityFramework问题,想请大家帮忙分析下:

我在尝试从SQL Server数据库查询Employee_Mail表数据时,代码直接抛出了SqlNullValueException,错误提示是:

"Data is Null. This method or property cannot be called on Null values."

对应的查询代码是这样的:

using (DbConnectionContext context = new DbConnectionContext(configuration)) //configuration -> IConfiguration
{
    return context.Employee_Mail.ToList();
}

我一开始怀疑是连接字符串的问题,但我有另一段插入数据的代码是能正常运行的。更离谱的是,当我把查询操作放到插入代码之后执行时,居然神奇地能正常返回所有员工数据了,连之前报错的查询逻辑都没问题了!

这段包含插入的代码如下:

using (DbConnectionContext context = new DbConnectionContext(configuration))
{
    var table = new Employee_Model()
    {
        USERNAME = "contoso@contoso.com",
        employee_typology = "some placeholder",
        employee_code = "1234",
        insert_date = DateTime.Now,
        ID_EMAIL = "0000",
    };

    context.Employee_Mail.Add(table);
    context.SaveChanges();

    return context.Employee_Mail.ToList();
}

我完全搞不懂这里的逻辑:为什么单独查询时会触发空值错误,而执行一次插入操作后,查询就完全正常了?这到底是哪里出了问题?

备注:内容来源于stack exchange,提问作者Cardo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.20 07:58:09