从数据库获取数据时对象引用错误

时间:2019-07-16 07:59:00

标签: asp.net-mvc entity-framework

我的门户已经运行了好几年,但是现在我的一些代码有问题。我想更新我的代码,但第一步,登录页面有问题。当我使用FirstOrDefault时,列表被填充并包含1个用户时,对象引用出现错误。

int g = db.sp_GetUserData_ByUserName(model.UserName.Trim()).Count();
//it shows 1 
OSUserData userData1 = db.sp_GetUserData_ByUserName(model.UserName.Trim()).FirstOrDefault();

我没有更改此代码,它可以在门户网站上完美运行。但是,我无法在开发服务器上运行它。

我再次检查,发现OSUserData构造函数抛出异常:枚举没有结果。这是我的课程代码:

  public OSUserData()
        {
            this.OSUploadedFilesMultipleUsers = new HashSet<OSUploadedFilesMultipleUser>();
            this.OSSalaryLists = new HashSet<OSSalaryList>();
            this.OSUploadedFiles = new HashSet<OSUploadedFile>();
        }

        public int Id { get; set; }
        public string Address { get; set; }
        public string BranchCode { get; set; }
        public string BranchParentNationalCode { get; set; }
        public string CitName { get; set; }
        public string City { get; set; }
        public string CompanyName { get; set; }
        public string CouName { get; set; }
        public string Country { get; set; }
        public string FaxNo { get; set; }
        public string FollowCode { get; set; }
        public string Hozeh { get; set; }
        public Nullable<bool> IsBranch { get; set; }
        public string LoginStatus { get; set; }
        public string NationalCode { get; set; }
        public string NewEconomicNo { get; set; }
        public string Oldeconomicno { get; set; }
        public string PostalCode { get; set; }
        public string ProIdTTMS { get; set; }
        public string ProName { get; set; }
        public string Provision { get; set; }
        public string RegistrationNo { get; set; }
        public string State { get; set; }
        public string TaxOfficeCode { get; set; }
        public string TaxPayerType { get; set; }
        public string TelCode { get; set; }
        public string TelNo { get; set; }
        public string TprId { get; set; }
        public string UserName { get; set; }
        public bool Validate { get; set; }
        public bool Disabled { get; set; }
        public string OwnerShipTypeDesc { get; set; }
        public Nullable<byte> OwnerShipTypeCode { get; set; }
        public bool Processed { get; set; }
        public bool CheckedUser { get; set; }
        public bool Blocked { get; set; }

        public virtual ICollection<OSUploadedFilesMultipleUser> OSUploadedFilesMultipleUsers { get; set; }
        public virtual ICollection<OSSalaryList> OSSalaryLists { get; set; }
        public virtual ICollection<OSUploadedFile> OSUploadedFiles { get; set; }

1 个答案:

答案 0 :(得分:0)

您正在尝试添加到空值/未引用/未初始化的对象/列表。首先初始化列表

List<OSUserData> userData1 = new List<OSUserData>();

然后添加

var user = db.sp_GetUserData_ByUserName(model.UserName.Trim()).FirstOrDefault();

userData1.Add(new OSUserData{
   Prop1 = user.Prop1,
   Prop2 = user.Prop2,
   ......
});

或者,如果您只想要一个用户,只需将其设为var

var user = db.sp_GetUserData_ByUserName(model.UserName.Trim()).FirstOrDefault();