EF Code First“事务问题”
在 asp.net Web 应用程序中使用最新的 EF Code First CTP,我似乎遇到了问题。请原谅缺少代码(暂时),但我会尽力解释清楚。
在我的控制器中,我通过 DI 容器注入存储库工厂和 IDataContext。 IDataContext 只是我的 DbContext 的一个接口,存储库工厂本身也将注入其中之一。
我的问题是,当从存储库(从工厂)检索用户,然后在 using(DataContext) 块(注入的 IDataContext)内添加一些属性时,更改不会被保存。
如果我创建一个新用户 - 效果很好。但是,当我将记录添加到用户的 ICollection 时,它们不会持续存在。
我创建了一个测试,使用一个 DataContext 创建用户,然后使用另一个 DataContext(两者都使用块)来添加记录。它工作得很好(在这个创建-处置-创建-处置序列中)。
我的猜测是,在控制器中,IDataContext 的不同实例正在影响发送到数据库的命令 - 甚至在那之前......
我明天应该解决这个问题。但如果我无法找到问题,任何建议仍然会很好。
Using the latest EF Code First CTP in an asp.net web app, I seem to have run into an issue. Please excuse the lack of code (for now) but I'll try to explain well.
In my controllers, I am injecting a repository factory and an IDataContext via my DI container. The IDataContext is just an interface to my DbContext and the repository factory itself will be injected with one of these also.
My problem is, when retrieving a user from the a repository (from the factory) and then adding some properties inside a using(DataContext) block - the IDataContext that is injected, the changes do not get saved.
If I create a new user - that works fine. But when I add Records to the user's ICollection they do not persist.
I created a test to use one DataContext to create the user, and then another DataContext - both in using blocks - to add records. And it works fine (in this create-dispose-create-dispose sequence).
My guess is that in the controllers, the different instances of IDataContext are affecting commands sent to the database - or even before then..
I should have the problem nailed tomorrow. But any suggestions would still be nice if I don't manage to track down the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法修复它。基本上我只需要告诉 DependencyResolver 我的 DbContext 实例是每个请求的。
希望这可以节省一些时间。
I managed to fix it. Basically I just needed to tell the DependencyResolver that instances of my DbContext were per-request.
Hope this saves somebody hassles some time.