c# - Nullable object must have a value, Linq to SQL, MVC issues -
i've taken on project developer has left company , not overly familiar mvc. when running program error "nullable object must have value".
one of cells in table populated following:
<td>@sra_acquire.models.auth.users.getusernamebyid((int)issue.userid)</td>
which populated 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 code userid correct, errors stating c.userid not exist in current context
however in accountmodels.cs
shows
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 missing?
try update c.userid == userid
condition you'll avoid such error:
c => c.userid != null && c.userid == userid
Comments
Post a Comment