ASP.Net MVC 3 - unitOfWork.Commit()不保存任何东西 [英] ASP.Net MVC 3 - unitOfWork.Commit() not saving anything
问题描述
我创建使用ASP.Net MVC 3和EF 4.1的Web应用程序,而我使用的模式的UnitOfWork,但没有得到被提交到数据库。这一切是相当新的给我,我不知道从哪里开始解决这个问题。
I created a web application using ASP.Net MVC 3 and EF 4.1, and I am using the UnitOfWork pattern, but nothing is getting committed to the database. All this is quite new to me, and I don't know where to start to resolve this issue.
根据我自己对这个职位创建我的Web应用程序:
I based myself on this post to create my web application:
<一个href=\"http://weblogs.asp.net/shijuvarghese/archive/2011/01/06/developing-web-apps-using-asp-net-mvc-3-razor-and-ef-$c$c-first-part-1.aspx\" rel=\"nofollow\">http://weblogs.asp.net/shijuvarghese/archive/2011/01/06/developing-web-apps-using-asp-net-mvc-3-razor-and-ef-$c$c-first-part-1.aspx
最后code,可以在这里得到 也有一个服务层和所述的UnitOfWork被注入到服务
The final code, which can be obtained here also has a service layer and the UnitOfWOrk is being injected into the services.
而不是采用基于统一2,因为他们在该项目的定制喷油器,我使用Unity.Mvc3。
Instead of using the custom injector based on Unity 2 as they are in that project, I am using Unity.Mvc3.
下面是我的IUnitOfWork类:
Here is my IUnitOfWork class:
public interface IUnitOfWork
{
void Commit();
}
和这里是我的UnitOfWork类:
And here is my UnitOfWork class:
public class UnitOfWork : IUnitOfWork
{
private readonly IDatabaseFactory databaseFactory;
private MyProjectContext dataContext;
public UnitOfWork(IDatabaseFactory databaseFactory)
{
this.databaseFactory = databaseFactory;
}
protected MyProjectContext DataContext
{
get { return dataContext ?? (dataContext = databaseFactory.Get()); }
}
public void Commit()
{
DataContext.Commit();
}
}
这是我的服务类的样子如何之一:
And here is how one of my service class look like:
public class RegionService : IRegionService
{
private readonly IRegionRepository regionRepository;
private readonly IUnitOfWork unitOfWork;
public RegionService(IRegionRepository regionRepository, IUnitOfWork unitOfWork)
{
this.regionRepository = regionRepository;
this.unitOfWork = unitOfWork;
}
...
}
在启动时,我的UnitOfWork组件正在注册这样的:
At start-up, my UnitOfWork component is being registered like this:
container.RegisterType<IUnitOfWork, UnitOfWork>();
现在,不管我是否尝试插入,更新或删除,没有请求被发送到数据库。什么是我在这里丢失?
Now, no matter whether I try to insert, update or delete, no request is being sent to the database. What am my missing here?
更新
下面是DataContext.Commit()的内容:
Here is the content of DataContext.Commit():
public class MyProjectContext : DbContext
{
public DbSet<Region> Regions { get; set; }
public virtual void Commit()
{
base.SaveChanges();
}
}
和这里是databaseFactory.Get():
And here is databaseFactory.Get():
public interface IDatabaseFactory : IDisposable
{
MyProjectContext Get();
}
更新#2:
使用调试器,我注意到,只有在执行时选择我区的服务和控制器构造越来越被调用一次,但是进行更新时,他们被称为两次。这是正常的吗?
Using the debugger, I am noticing that my Region service and controller constructors are getting called once when performing only a select, but they are called twice when performing an update. Is this normal?
推荐答案
好吧,我发现罪魁祸首。它与我如何注册我的数据库厂做。
Ok, I found the culprit. It has to do with how I was registering my database factory.
而不是
container.RegisterType<IDatabaseFactory, DatabaseFactory>();
我需要
container.RegisterType<IDatabaseFactory, DatabaseFactory>(new HierarchicalLifetimeManager());
我发现本网站上的信息:
I found the information on this web site:
<一个href=\"http://www.devtrends.co.uk/blog/introducing-the-unity.mvc3-nuget-package-to-reconcile-mvc3-unity-and-idisposable\" rel=\"nofollow\">http://www.devtrends.co.uk/blog/introducing-the-unity.mvc3-nuget-package-to-reconcile-mvc3-unity-and-idisposable
这篇关于ASP.Net MVC 3 - unitOfWork.Commit()不保存任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!