我无法从Application Identity Users读取数据。我是初学者,任何想法?编译时没有错误。但我无法添加新任务。
我在创建任务时遇到错误https://localhost:44312/usertasks/Create
问题在于GET方法,但用户lis不为空
System.NullReferenceException:未将对象引用设置为实例 一个物体 在
Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetListItemsWithValueField()
在Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetListItems()
在Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetEnumerator()
public class UserTask
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserTaskID { get; set; }
public string Description { get; set; }
[ForeignKey("OwnerUserID")]
public string TaskOwnerId { get; set; }
[ForeignKey("ExecutorUserID")]
public string TaskExecutorId { get; set; }
public virtual ApplicationUser OwnerUserID { get; set; }
public virtual ApplicationUser ExecutorUserID { get; set; }
}
public class ApplicationUser : IdentityUser
{
public ICollection<UserTask> OwnedUserTasks { get; set; }
public ICollection<UserTask> ExecutedUserTasks { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<UserTask> UserTask { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>()
.HasMany(x => x.OwnedUserTasks)
.WithOne(x => x.OwnerUserID);
builder.Entity<ApplicationUser>()
.HasMany(x => x.ExecutedUserTasks)
.WithOne(x => x.ExecutorUserID);
}
}
Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc();
}
}
UserTaskControler
public class UserTasksController : Controller
{
private readonly ApplicationDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;
public UserTasksController(ApplicationDbContext context, UserManager<ApplicationUser> userManager)
{
_context = context;
_userManager = userManager;
}
public IActionResult Create()
{
ViewData["TaskOwnerId"] = new SelectList(_userManager.Users.ToList(), "Id", "Name");
ViewData["TaskExecutorId"] = new SelectList(_userManager.Users.ToList(), "Id", "Name");
return View();
}
}
和视图
@model UserTaskTest.Models.UserTask
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<h4>UserTask</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Description" class="control-label"></label>
<input asp-for="Description" class="form-control" />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="TaskOwnerId" class="control-label"></label>
<select asp-for="TaskOwnerId" class="form-control" asp-items="ViewBag.TaskOwnerId"></select>
</div>
<div class="form-group">
<!-- <label asp-for="TaskExecutorId" class="control-label"></label>
<select asp-for="TaskExecutorId" class="form-control" asp-items="ViewBag.TaskExecutorId"></select> -->
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
答案 0 :(得分:0)
_userManager导致此问题。这是因为更改跟踪以及您尝试更改ForeignKeys
的{{1}}的性质。尝试使用User
填充您的下拉列表,虽然它在生成的上下文中不显示,但它是继承礼物。
除非您在ApplicationUser中实际创建了该属性,否则您的下拉列表中的_dbContext.Users.ToList()
将会出错,因为默认情况下它不存在。 https://github.com/aspnet/Identity/blob/dev/src/Microsoft.Extensions.Identity.Stores/IdentityUser.cs
Name