C# 实体框架6可以';找不到错误列

C# 实体框架6可以';找不到错误列,c#,sql,asp.net-mvc,entity-framework,C#,Sql,Asp.net Mvc,Entity Framework,我将MVC与实体框架一起使用,出现以下错误 从物化的“System.Int64”类型到“System.String”类型的指定强制转换无效 这个错误很容易理解,也很容易解决,但我面临的问题是,在我的SQL查询中,我有很多列,从错误详细信息中,我看不出哪个列有这个特定的问题,我必须逐个检查所有列 string Query= "select id,claim_no,emp_id,dept_id,location_id from staff"; var ctx = new TIAEntities()

我将MVC与实体框架一起使用,出现以下错误

从物化的“System.Int64”类型到“System.String”类型的指定强制转换无效

这个错误很容易理解,也很容易解决,但我面临的问题是,在我的SQL查询中,我有很多列,从错误详细信息中,我看不出哪个列有这个特定的问题,我必须逐个检查所有列

string Query= "select id,claim_no,emp_id,dept_id,location_id from staff";
var ctx = new TIAEntities()
ctx.Database.SqlQuery<ORM>(Query).ToList()

您可以使用以下解决方案:

var query = from o in ctx.ORM 
            select new 
            {
                id = o.id,
                claim_no = o.claim_no,
                emp_id = o.emp_id,
                dept_id = o.dept_id,
                location_id = o.location_id
            };

查询的类型为:
可查询查询

若要查找此属性中的哪一个会引发使用此解决方案的错误,请执行以下操作:

  • 在ActionResult方法中,为类似这样的“if(ModelState.IsValid)”添加else,以在回发模式下从视图中获取问题,并在else部分中设置断点,在断点停止后,使用立即窗口将错误值设置为类似于以下错误的值[0]

        if (ModelState.IsValid)
        {
            ...
        }
        else
        {
            var errors = ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => new
            {
                x.Key,
                x.Value.Value,
                x.Value.Errors
            }).ToArray();
        }
    
  • 使用此try catch查找属性的错误并点击该按钮

        try
        {
            var query = from o in ctx.ORM 
                select new 
                {
                    id = o.id,
                    claim_no = o.claim_no,
                    emp_id = o.emp_id,
                    dept_id = o.dept_id,
                    location_id = o.location_id
                };
        }
        catch (DbEntityValidationException ex)
        {
            var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);
            var fullErrorMessage = string.Join("; ", errorMessages);
            var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
            throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
        }
    

  • 如果没有模型或表定义,我们应该如何为您找到列名。把这些加到你的问题上。我已经编辑过了,请现在查看。很可能,因为它说它不能从
    Int64
    转换到
    String
    声明号
    很可能存储为bigint。这是我编写的唯一一个测试用例,在我的原始ORM中,我总共有72列,并且都混合了不同的数据类型。我如何才能得到有问题的列名?所以。。。没有人在这方面提供帮助@专业知识,需要你的专注。
        if (ModelState.IsValid)
        {
            ...
        }
        else
        {
            var errors = ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => new
            {
                x.Key,
                x.Value.Value,
                x.Value.Errors
            }).ToArray();
        }
    
        try
        {
            var query = from o in ctx.ORM 
                select new 
                {
                    id = o.id,
                    claim_no = o.claim_no,
                    emp_id = o.emp_id,
                    dept_id = o.dept_id,
                    location_id = o.location_id
                };
        }
        catch (DbEntityValidationException ex)
        {
            var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);
            var fullErrorMessage = string.Join("; ", errorMessages);
            var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
            throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
        }