【问题标题】:A member of the type, 'aim1_Accuracy', does not have a corresponding column in the data reader with the same name类型的成员“aim1_Accuracy”在数据读取器中没有同名的对应列
【发布时间】:2019-01-16 14:26:05
【问题描述】:

我正在将我的 asp.net mvc 项目与我的 sql server 数据库连接,但是当我添加最后一列时,它显示:

数据读取器与指定的“AIRDMIS.Models.R1T_CDC”不兼容。类型的成员“aim1_Accuracy”在数据读取器中没有同名的对应列 只有一列不工作,其余显示正常

我的代码

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace AIRDMIS.Models
{
     [Table("R1T_CDC")]
    public class R1T_CDC
    {
        [Key]
        public string Meta_instanceID { get; set; }
        public double? S_No { get; set; }
        public DateTime? SubmissionDate { get; set; }
        public double? aim1_Latitude { get; set; }
        public double? aim1_Longitude { get; set; }
        public double? aim1_Altitude { get; set; }
        [Column("[aim1-Accuracy]")]
        public double? aim1_Accuracy { get; set; }
    }
}
    public class Context : DbContext
    {
        public Context() : base("DefaultConnection") { }
        public DbSet<R1T_CDC> R1T_CDC { get; set; }
    }
}

我的控制器

        public ActionResult About()
        {
            var data = db.Database.SqlQuery<R1T_CDC>(@"select * from R1T_CDC");
            return Json(data, JsonRequestBehavior.AllowGet);
        }

【问题讨论】:

  • 我认为您需要删除此注释:'[Column("[aim1-Accuracy]")]' 或将其更改为 '[Column("[aim1_Accuracy]")]'
  • 还是同样的错误
  • 您是否创建并应用了迁移?
  • 前 6 列工作得很好,但是当我添加最后一列时它给出了错误
  • 不确定,因为我还没有使用 Column 属性,但也许你需要去掉括号?当然,由于连字符,您在查询管理器中需要它们,但在这里您明确表示它是一个列名,所以我怀疑您在这里需要它们。

标签: c# asp.net-mvc entity-framework


【解决方案1】:

不确定可能是什么问题,但我可能会尝试在 SQL 查询中枚举您选择的对象。如果您枚举该字段,然后得到该字段在表中不存在的错误,则表明该错误与数据库表有关。

var data = db.Database.SqlQuery<R1T_CDC>(@"select S_No, SubmissionDate, aim1_Latitude, aim1_longitude, aim1_altitude, [aim1-Accuracy] from R1T_CDC");

(另外,为什么在您的数据库表中,aim1-Accuracy 列的名称周围有括号?)。可以去掉括号看看是否还有错误吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    相关资源
    最近更新 更多