[英]Identity user in new entity
I am trying to make an ASP.NET MVC Project. 我正在尝试制作一个ASP.NET MVC项目。 I have a forum controller with a SubmitPost
action. 我有一个带有SubmitPost
操作的论坛控制器。 Here is the code for the action: 这是操作的代码:
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Submit(PostSubmitViewModel model)
{
if (!this.ModelState.IsValid)
{
return View(model);
}
var category = this.categoryService.GetById(model.CategoryId);
var user = this.userManager.FindById(this.User.Identity.GetUserId());
this.postService.Submit(new Post
{
Title = model.Title,
Content = model.Content,
Category = category,
Author = user,
});
return RedirectToAction("Category", "Forum", new { id = model.CategoryId });
}
As you can see there is a postService
and userManager
. 如您所见,这里有一个postService
和userManager
。 They are injected by Ninject and they both use the same DbContext - per request scope. 它们由Ninject注入,并且它们都使用相同的DbContext-每个请求范围。 Login, Register all work as expected. 登录,按预期注册所有工作。
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ApplicationDbContext>().ToSelf().InRequestScope();
// Identity bindings
kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>().InRequestScope()
.WithConstructorArgument<DbContext>(kernel.Get<ApplicationDbContext>());
kernel.Bind<IAuthenticationManager>().ToMethod(c => HttpContext.Current.GetOwinContext().Authentication).InRequestScope();
kernel.Bind<ApplicationUserManager>().ToSelf().InRequestScope();
kernel.Bind<ApplicationSignInManager>().ToSelf().InRequestScope();
}
The post service does only this: 邮政服务仅执行以下操作:
public void Submit(Post post)
{
this.context.Posts.Add(post);
this.context.SaveChanges();
}
My problem is that when the action is called i get an exception: 我的问题是,当调用该动作时,我得到一个例外:
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
at this line: 在这一行:
this.context.Posts.Add(post);
I'm pretty sure i have only one dbContext instance for both the service and userManager. 我很确定我对于服务和userManager都只有一个dbContext实例。 When I insert the post with a null
author it works fine. 当我使用null
作者插入帖子时,它可以正常工作。 I guess EF is trying to save the existing user again but I don't get the same error with the category that also comes from an external service and already exists. 我猜想EF试图再次保存现有用户,但是在类别也来自外部服务且已经存在的类别中,我没有得到相同的错误。 I tried putting a breakpoint on MyDbContext
's constructor and it's only called once. 我尝试在MyDbContext
的构造函数上放置一个断点,并且只调用一次。 Maybe there is something specific about UserManager that I don't know about? 也许我不知道有关UserManager的某些特定信息?
Here is my user manager class: 这是我的用户管理器类:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
// Validation configs etc.
I have no idea what exactly is causing the exception so any ideas would be helpful. 我不知道到底是什么引起异常,所以任何想法都会有所帮助。
With limited access to the code I can only suggest: 在对代码的访问受限的情况下,我只能建议:
on the object Post
create a property AuthorId
在对象Post
创建属性AuthorId
Then change 然后改变
var user = this.userManager.FindById(this.User.Identity.GetUserId());
this.postService.Submit(new Post
{
Title = model.Title,
Content = model.Content,
Category = category,
Author = user,
});
to 至
var userId = this.User.Identity.GetUserId();
this.postService.Submit(new Post
{
Title = model.Title,
Content = model.Content,
Category = category,
AuthorId = userId,
});
If the above doesn't work, on the class Post
decorate the property Author
with ForeignKey(nameof(AuthorId))
如果上述方法不起作用,则在Post
类上用ForeignKey(nameof(AuthorId))
装饰属性Author
。
this is all i can give at the moment without more information. 这是我目前所能提供的,没有更多信息。 I hope it helps 希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.