[英]How to merge Identity dbcontext with application context?
I'm not able to read data from Application Identity Users. 我无法从“应用程序标识用户”中读取数据。 I'm beginner, any idea?
我是新手,有什么主意吗? There is no error while compiling.
编译时没有错误。 But I cannot add new task.
但是我无法添加新任务。
I get the error while creating a task https://localhost:44312/usertasks/Create 创建任务https:// localhost:44312 / usertasks / Create时出现错误
The issue is on GET method but the user lis is not empty 问题在GET方法上,但用户lis不为空
System.NullReferenceException: Object reference not set to an instance of an object.
System.NullReferenceException:对象引用未设置为对象的实例。
at在
Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetListItemsWithValueField()Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetListItemsWithValueField()
at Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetListItems()在Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetListItems()
at Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetEnumerator()在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 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();
}
}
and the 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");}
}
_userManager is causing the issue here. _userManager导致了此问题。 It's assuming because of change tracking and the nature of your
ForeignKeys
that you are trying to alter the User
. 假定由于更改跟踪和
ForeignKeys
的性质,您试图更改User
。 Attempt to fill your Dropdowns with _dbContext.Users.ToList()
, while it doesn't show available in the context that is generated, it's an inheritance gift. 尝试用
_dbContext.Users.ToList()
填充您的下拉_dbContext.Users.ToList()
,尽管在生成的上下文中它不显示可用,但这是继承礼物。
Also Name
in your dropdown will error unless you actually created the property in ApplicationUser, since by default it doesn't exist. 除非您在ApplicationUser中实际创建了属性,否则下拉列表中的
Name
也会出错,因为默认情况下该属性不存在。 https://github.com/aspnet/Identity/blob/dev/src/Microsoft.Extensions.Identity.Stores/IdentityUser.cs https://github.com/aspnet/Identity/blob/dev/src/Microsoft.Extensions.Identity.Stores/IdentityUser.cs
public class ApplicationUser : IdentityUser
{
public string Name {get;set;} //?? missing?
public ICollection<UserTask> OwnedUserTasks { get; set; }
public ICollection<UserTask> ExecutedUserTasks { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.