[英]Why Entity Framework doesn't initialize field?
I created two entities, added them to the data context, created a relationship between them, performed the migration.我创建了两个实体,将它们添加到数据上下文中,在它们之间创建了关系,执行了迁移。 After registering the user, the application created his record in the database and established a connection with the default image.
注册用户后,应用程序在数据库中创建他的记录并与默认图像建立连接。 During the next request -
ProfilePictureId
is initialized by the correct value, but ProfilePicture
is null.在下一个请求期间 -
ProfilePictureId
由正确的值初始化,但ProfilePicture
为空。
User.cs用户.cs
public class User: IdentityUser
{
public int? ProfilePictureId { get; set; }
public virtual Image ProfilePicture { get; set; }
}
Image.cs图像文件
public class Image
{
public int Id { get; set; }
public string Filename { get; set; }
}
RegisterModel.cshtml.cs注册模型.cshtml.cs
[AllowAnonymous]
public class RegisterModel : PageModel
{
private readonly SignInManager<User> _signInManager;
private readonly UserManager<User> _userManager;
private readonly UserSettings _userSettings;
private readonly ApplicationDbContext _dbContext;
private readonly ILogger<RegisterModel> _logger;
private readonly IEmailSender _emailSender;
public RegisterModel(
UserManager<User> userManager,
SignInManager<User> signInManager,
IOptions<UserSettings> userSettings,
ApplicationDbContext dbContext,
ILogger<RegisterModel> logger,
IEmailSender emailSender)
{
_userManager = userManager;
_signInManager = signInManager;
_userSettings = userSettings.Value;
_dbContext = dbContext;
_logger = logger;
_emailSender = emailSender;
}
// ...
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
// ...
if (ModelState.IsValid)
{
// var user = new User ...
SetUserDefaultParams(user);
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
// ...
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
// If we got this far, something failed, redisplay form
return Page();
}
private void SetUserDefaultParams(User user)
{
// ...
if (_userSettings != null)
{
var image = _dbContext.Images
.Where(
i => i.Filename == _userSettings.DefaultProfilePicture)
.FirstOrDefault();
user.ProfilePicture = image; // this always returns the desired value (not null)
}
_userManager.UpdateAsync(user).Wait();
}
}
I expect ProfilePicture to be not null in _LoginPartial.cshtml
Razor-page:我希望 ProfilePicture 在
_LoginPartial.cshtml
Razor-page 中不为空:
@inject UserManager<User> UserManager
@{
var user = UserManager.GetUserAsync(User).Result;
var pathToProfilePicture = string.Empty;
if (user != null)
{
pathToProfilePicture =
$"../../Resources/Images/{user.ProfilePicture.Filename}"; // here
// I get NullReferenceException
}
}
Try adding .Include(e => e.ProfilePicture)
in your statement.尝试在您的语句中添加
.Include(e => e.ProfilePicture)
。 https://docs.microsoft.com/en-us/ef/ef6/querying/related-data https://docs.microsoft.com/en-us/ef/ef6/querying/related-data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.