简体   繁体   English

新实体中的身份用户

[英]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 . 如您所见,这里有一个postServiceuserManager 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.

相关问题 在实体框架中获取新记录的标识列? - Getting Identity Column of new record in Entity Framework? 使用标识主键将新实体插入上下文 - Insert new entity to context with identity primary key 定制新的用户上下文标识时无服务 - No service while customize a new User Context Identity 模拟新的 Microsoft Entity Framework Identity UserManager 和 RoleManager - Mocking new Microsoft Entity Framework Identity UserManager and RoleManager Identity Framework 2不会对新的自定义实体应用更改 - Identity Framework 2 does not apply changes on new custom entity ASp.net实体框架多个身份用户实体 - ASp.net Entity Framework multiple identity user entities 如何正确实现其他实体对身份用户的引用? - How to correctly implement other entity's reference to Identity user? 身份用户 object 未映射到另一个 object - 实体框架 - Identity user object is not mapped to another object - entity framework 实体框架6添加了新的导航实体,导致“无法插入显式身份”错误 - Entity Framework 6 adding new navigation entity causing “cannot insert explicit identity” error 当由entity的id创建一个属性时,如何插入新的entity? (id表示身份证号码) - How to insert new entity when one property was created by id of entity ? (id means identity number)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM