好的,我在ApplicationUser和QuestionResults之间有一个关系,我的模型如下,userId和UserName被检索,但我真的需要在QuestionResults实体上将UserId设置为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;
}
}
答案 0 :(得分:2)
您的代码与此::
等效ShopId
如果var user = qr.User;
user.Id = User.Identity.GetUserId();
已经与QuestionResult
相关联,那么您不会更改哪个User
与User
相关联,那么您将更改QuestionResult
现有的Id
- 并且无论如何都不允许这样做。
但User
尚未与QuestionResult
相关联。 User
为空 - 因此您将获得空引用异常。
通常,如果将外键添加到模型中,实体框架中的生命会更容易:
qr.User
现在您可以直接设置外键:
public class QuestionResult
{
public string UserId { get; set; }
public virtual ApplicationUser User { get; set; }
}
参考文献:
Why does Entity Framework Reinsert Existing Objects into My Database?
答案 1 :(得分:0)
那么,你想为用户标识做出预测关键吗?
你可以这样做:
<api xmlns="http://ws.apache.org/ns/synapse" name="sample" context="/api/sample">
<resource methods="OPTIONS GET" uri-template="/{val1}/groups/{val2}.json?q1={v1}&q2={v2}">
<inSequence>
<property name="uri.var.q1" expression="$url:q1"></property>
<property name="uri.var.q2" expression="$url:q2"></property>
<property name="uri.var.val1" expression="get-property('uri.var.val1')"></property>
<property name="uri.var.val2" expression="get-property('uri.var.val2')"></property>
<send>
<endpoint>
<http method="GET" uri-template=""></http>
</endpoint>
</send>
</inSequence>
<outSequence>
<send></send>
</outSequence>
</resource>
</api>
出现此错误,因为您对模型或数据类有一些问题。
答案 2 :(得分:-1)
将其设为字符串
型号:
public ApplicationUser User { get; set; }
public string UserId { get; set; }
控制器:
qr.UserId = User.Identity.GetUserId();
工作得很完美,甚至创造了外键,这很容易。非常感谢!