繁体   English   中英

使用User.Identity.GetUserId的ASP MVC应用程序用户为空

[英]ASP MVC Application User Null using User.Identity.GetUserId

好的,所以我在ApplicationUser和QuestionResults之间有一个关系,我的模型如下所示,检索到了UserId或UserName,但是我确实需要UserId设置作为QuestionResults实体上的ForeignKey。

非常感谢任何帮助 ,我收到的错误如下:

An exception of type 'System.NullReferenceException' occurred in STRA.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.
on these lines of code:

qr.User.Id = User.Identity.GetUserId();
qr.User.UserName = User.Identity.GetUserName();

楷模

public class QuestionResult
    {
        public QuestionResult()
        {
            DateCreated = DateTime.Now;
            DateModified = DateTime.Now;
        }
        public int Id { get; set; }
        public DateTime? DateCreated { get; set; }
        public DateTime? DateModified { get; set; }
        public int QuestionScore { get; set; }
        //navigation properties
        public virtual ApplicationUser User { get; set; }
        //public ICollection<CategoryResult> CategoryResult { get; set; }
        //public virtual Category Category { get; set; }
        [NotMapped]
        public int CategoryId { get; set; }
        public virtual Question Question { get; set; }
        public int QuestionId { get; set; }
        //public virtual Category Category { get; set; }
        //public int CategoryId { get; set; }
    }
public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public string Industry { get; set; }
        public string GlobalRegion { get; set; }
        public string CurrentSituation { get; set; }
        public int SalesForceSize { get; set; }
        public bool IsVerified { get; set; }
        //navigation properties
        public virtual ICollection<CategoryResult> CategoryResult { get; set; }
        public virtual ICollection<QuestionResult> QuestionResult { get; set; }
        //public virtual ICollection<Report> Report { get; set; }
        //public virtual ICollection<SurveyResult> SurveyResult { get; set; }
        public virtual Organisation Organisation { get; set; }
        public int? OrganisationId { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

    }

您的代码与此等效:

var user = qr.User;
user.Id = User.Identity.GetUserId();

如果QuestionResult已经链接到User ,那么你就不会改变其User链接到QuestionResult ,您将得到改变Id现有的User -这无论如何都不会允许的。

但是QuestionResult尚未链接到User qr.Userqr.User因此您将获得null引用异常。

通常,如果将外键添加到模型中,则在Entity Framework中,生活会容易得多:

public class QuestionResult
{
   public string UserId { get; set; }

   public virtual ApplicationUser User { get; set; }
}

现在,您可以直接设置外键:

qr.UserId = User.Identity.GetUserId();

参考文献:

为什么实体框架将现有对象重新插入到我的数据库中?

使用缺少的外键

因此,您是否想为userid设置密钥?

您可以这样做:

    public int UserRefID { get; set; }
    [ForeignKey("UserRefID")]
    public xxx UserID { get; set; } //data name like ApplicationUser

而且出现此错误是因为您对模型或数据类存在一些问题。

将其设置为字符串

模型:

public ApplicationUser User { get; set; }
public string UserId { get; set; }

控制器:

qr.UserId = User.Identity.GetUserId();

并完美地工作,甚至创建外键也是如此。 非常感谢!

暂无
暂无

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

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