“实体对象不能被 IEntityChangeTracker 的多个实例引用”使用自定义模型活页夹
我一直在关注达林的回答,回答我关于瘦身控制器的问题,但我遇到了问题IEntityChangeTracker
异常:
一个实体对象不能被 IEntityChangeTracker 的多个实例引用。
这是我的模型活页夹:
public class PostModelBinder : IModelBinder
{
private readonly IPostRepository _repository;
public PostModelBinder(IPostRepository repository)
{
_repository = repository;
}
public object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
var postIDValue = controllerContext.Controller.ValueProvider.GetValue("postID");
int postID;
if (postIDValue == null || !int.TryParse(postIDValue.AttemptedValue, out postID))
return null;
return _repository.FindPost(postID, filterByPublished: true);
}
}
在此之后,我的自定义操作过滤器仅对帖子进行一些验证,并在帖子无效时进行重定向。到目前为止,一切都很好。当我尝试更新帖子读取计数时,我的控制器中发生了错误:
public class PostsController : Controller
{
private IPostRepository postRepository;
// snip...
[AutoMap(typeof(Post), typeof(PostViewModel)), HttpGet]
[PostActionFilter]
public ActionResult ShowPost(Post model)
{
postRepository.PostVisited(model);
return View(model);
}
}
我理解 IEntityChangeTracker
的抱怨,因为我实际上最终得到了 2 个引用同一对象的存储库,而这似乎是糟糕的魔力等待发生。我知道我可以将 PostVisited()
调用推送到 PostModelBinder
但更新模型似乎不属于那里的行为。
还有其他办法解决这个问题吗?
谢谢。
I've been following Darin's answer to a question I had on slimming down my controllers and I'm running into problems with this IEntityChangeTracker
exception:
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
Here's my model binder:
public class PostModelBinder : IModelBinder
{
private readonly IPostRepository _repository;
public PostModelBinder(IPostRepository repository)
{
_repository = repository;
}
public object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
var postIDValue = controllerContext.Controller.ValueProvider.GetValue("postID");
int postID;
if (postIDValue == null || !int.TryParse(postIDValue.AttemptedValue, out postID))
return null;
return _repository.FindPost(postID, filterByPublished: true);
}
}
After this, my custom action filter simply carries out a bit of validation on the post and redirects if the post isn't valid. Up until this point, everything is fine. The error happens in my controller when I try to update the post read count:
public class PostsController : Controller
{
private IPostRepository postRepository;
// snip...
[AutoMap(typeof(Post), typeof(PostViewModel)), HttpGet]
[PostActionFilter]
public ActionResult ShowPost(Post model)
{
postRepository.PostVisited(model);
return View(model);
}
}
I understand IEntityChangeTracker
's complaining because I effectively end up with 2 repositories referencing the same object and that just seems like bad mojo waiting to happen. I know I could push the PostVisited()
call into PostModelBinder
but updating a model doesn't seem like behaviour that belongs there.
Is there another way around this?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从错误中,我假设您的两个 IPostRepository 实现对象使用不同的实体框架对象上下文;您可以通过在整个请求中使用相同的对象上下文来解决此问题。
某些 DI 容器支持每个请求的对象生命周期,否则您可以延迟加载对象上下文并将其存储在
HttpContext.Items
中,并在请求结束时对其进行处置,如 这个问题。From the error, I assume your two
IPostRepository
implementation objects use different Entity Framework object contexts; you can fix this by using the same object context throughout a request.Some DI containers support per-request object lifetimes, otherwise you can lazy-load and store your object context in
HttpContext.Items
and dispose of it at the end of the request, as mentioned in this question.