C# 尽管我的模型类都是字符串类型,但为什么我一直得到这个空数据异常?

C# 尽管我的模型类都是字符串类型,但为什么我一直得到这个空数据异常?,c#,entity-framework-core,C#,Entity Framework Core,System.InvalidCastException:“列包含空数据” 谁能帮我一下吗?我不断得到列包含空数据异常。我的模型类都是字符串类型,为什么会出现这个错误 此异常最初是在此调用堆栈中引发的: [External Code] LXG.DataAccess.Repository.Repository<T>.GetALL(System.Linq.Expressions .Expression<System.Func<T, bool>>, System.

System.InvalidCastException:“列包含空数据”

谁能帮我一下吗?我不断得到列包含空数据异常。我的模型类都是字符串类型,为什么会出现这个错误

此异常最初是在此调用堆栈中引发的:

[External Code]
LXG.DataAccess.Repository.Repository<T>.GetALL(System.Linq.Expressions
  .Expression<System.Func<T, bool>>, System.Func<System.Linq.IQueryable<T>,
  System.Linq.IOrderedQueryable<T>>, string) in Repository.cs
LXG.Areas.Admin.Controllers.CMJobProgController.GetAll() in CMJobProgController.cs
[External Code]
CMJobProgController.cs

public IActionResult GetAll()
{
     //this one no problem
    var allObj = _unitOfWork.cmJobProg.GetALL(j => j.JobNumber == "19950232");

    //this one throws the exception
    var pubcode = _unitOfWork.pubcode.GetALL(s => s.CodeType == "JPA"); 

    return Json(new { data = allObj });
}
Repository.cs

public IEnumerable<T> GetALL(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = null)
{
    IQueryable<T> query = dbSet;

    if (filter != null)
    {
        query = query.Where(filter);
    }

    if (includeProperties != null)
    {
        foreach (var includeProp in includeProperties.Split(",", StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProp);
        }
    }

    if (orderBy != null)
    {
        return orderBy(query).ToList();
    }

    return query.ToList();
}
public IEnumerable GetALL(表达式筛选器=null,Func orderBy=null,字符串includeProperties=null)
{
IQueryable query=dbSet;
if(过滤器!=null)
{
query=query.Where(过滤器);
}
if(includeProperties!=null)
{
foreach(includeProperty.Split(“,”,StringSplitOptions.RemoveEmptyEntries中的变量includeProp))
{
query=query.Include(includeProp);
}
}
if(orderBy!=null)
{
returnorderby(query.ToList();
}
返回query.ToList();
}
必须返回到ToList()或FirstOrDefault()。如果您的字段是设置为FirstOrDefault()的perimary密钥,如果不是perimary密钥,请设置为list()

或者您可以使用其他方式从pubcode表获取数据
如果代码类型不是主键

var pubcode = _unitOfWork.pubcode.Where(s => s.CodeType == "JPA").ToList();
var pubcode = _unitOfWork.pubcode.Where(s => s.CodeType == "JPA").FirstOrDefault();
如果代码类型是主键

var pubcode = _unitOfWork.pubcode.Where(s => s.CodeType == "JPA").ToList();
var pubcode = _unitOfWork.pubcode.Where(s => s.CodeType == "JPA").FirstOrDefault();

哪个栏目?我想这可能与所有列上的
[必需]
属性有关。是的。我删除了请求,那么它就不会有错误。。因为我检索数据,以前的数据为空。非常感谢雅各布·巴斯克·索伦森。我对.NETCore真的很陌生。