简体   繁体   English

可空对象必须具有一个值,Linq to SQL,MVC问题

[英]Nullable object must have a value, Linq to SQL, MVC issues

I've taken over a project for a developer who has left the company and am not overly familiar with MVC. 我已经为一个已经离开公司并且对MVC不太熟悉的开发人员接手了一个项目。 When running the program I get the error that "Nullable object must have a value". 运行程序时,出现错误“可为空的对象必须具有值”。

One of the cells in a table is populated by the following: 表中的单元格之一由以下填充:

<td>@SRA_Acquire.Models.Auth.Users.GetUserNameById((int)issue.UserID)</td>

Which is populated by the following: 由以下内容填充:

public static string GetUserNameById(int userId)
{
    return GetUserById(userId).Name;
}

internal static UserProfile GetUserById(int userId)
{
    var db = new UsersContext();
    return db.UserProfiles.Where(c => c.UserId == userId).Single();
}

When stepping through the code the userId is correct, but I get errors stating that c.UserID does not exist in the current context 单步执行代码时,userId是正确的,但是c.UserID does not exist in the current context显示c.UserID does not exist in the current context出现错误

However in my AccountModels.cs it shows 但是在我的AccountModels.cs它显示

public class UsersContext : DbContext
{
    public UsersContext()
        : base("AuthContext")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<UsersInRoles> UsersInRoles { get; set; }
    public DbSet<Roles> Roles { get; set; }
}

[Table("UserProfile", Schema = "dbo")]
public class UserProfile 
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public bool IsLockedOut { get; set; }
}

What am I missing? 我想念什么?

尝试更新c.UserId == userId条件,以便避免此类错误:

c => c.UserId != null && c.UserId == userId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM