【发布时间】:2013-06-26 12:03:40
【问题描述】:
我对实体框架非常陌生,所以如果这是一个愚蠢的问题,我深表歉意......
这是我的 POCO:
[Table("RegistrationCodes")]
public class RegistrationCode
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public Guid Code { get; set; }
public int? UserId { get; set; }
[ForeignKey("UserId")]
public UserProfile User { get; set; }
[StringLength(500)]
public string Notes { get; set; }
}
当我使用下面的代码从我的 DBSet 中检索对象时,UserId 始终为空。
return this.Context.RegistrationCodes.FirstOrDefault(c => c.Code == code);
我检查了数据库并确认 UserId 不为空。此外,我已经分析并且可以看到正在从数据库中请求 UserId。
任何帮助将不胜感激。
【问题讨论】:
-
是空的 UserId 还是 UserProfile?
-
UserId 和 UserProfile 都是 null 是 null