ASP.Net MVC 3 - unitOfWork.Commit() 不保存任何内容

发布于 2024-12-02 20:37:27 字数 2282 浏览 1 评论 0 原文

我使用 ASP.Net MVC 3 和 EF 4.1 创建了一个 Web 应用程序,并且使用了 UnitOfWork 模式,但没有任何内容提交到数据库。这一切对我来说都是全新的,我不知道从哪里开始解决这个问题。

我根据这篇文章创建了我的网络应用程序:

http://weblogs.asp.net/shijuvarghese/archive/2011/01/06/developing-web-apps-using-asp-net-mvc-3-razor-and-ef-code- first-part-1.aspx

最终的代码,可以在这里获取层并且 UnitOfWOrk 正在被注入到服务中。

我没有像该项目中那样使用基于 Unity 2 的自定义注入器,而是使用 Unity.Mvc3。

这是我的 IUnitOfWork 类:

public interface IUnitOfWork
{
    void Commit();
}

这是我的 UnitOfWork 类:

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();
    }
}

这是我的一个服务类的样子:

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 组件是这样注册的:

container.RegisterType<IUnitOfWork, UnitOfWork>();

现在,无论我是否尝试插入、更新或删除,没有请求发送到数据库。我在这里缺少什么?

更新

这是DataContext.Commit()的内容:

public class MyProjectContext : DbContext
{
     public DbSet<Region> Regions { get; set; }

    public virtual void Commit()
    {
        base.SaveChanges();
    }
}

这是databaseFactory.Get():

public interface IDatabaseFactory : IDisposable
{
    MyProjectContext Get();
}

更新#2:

使用调试器,我注意到我的区域服务和控制器构造函数在仅执行选择时被调用一次,但在执行更新时被调用两次。这是正常的吗?

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.

I based myself on this post to create my web application:

http://weblogs.asp.net/shijuvarghese/archive/2011/01/06/developing-web-apps-using-asp-net-mvc-3-razor-and-ef-code-first-part-1.aspx

The final code, which can be obtained here also has a service layer and the UnitOfWOrk is being injected into the services.

Instead of using the custom injector based on Unity 2 as they are in that project, I am using Unity.Mvc3.

Here is my IUnitOfWork class:

public interface IUnitOfWork
{
    void Commit();
}

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;
    }  
    ...
}

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?

UPDATE:

Here is the content of DataContext.Commit():

public class MyProjectContext : DbContext
{
     public DbSet<Region> Regions { get; set; }

    public virtual void Commit()
    {
        base.SaveChanges();
    }
}

And here is databaseFactory.Get():

public interface IDatabaseFactory : IDisposable
{
    MyProjectContext Get();
}

UPDATE #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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绮烟 2024-12-09 20:37:27

好吧,我找到罪魁祸首了。这与我注册数据库工厂的方式有关。

我没有

container.RegisterType<IDatabaseFactory, DatabaseFactory>();

所需的

container.RegisterType<IDatabaseFactory, DatabaseFactory>(new HierarchicalLifetimeManager());

在这个网站上找到

信息: http://www.devtrends.co.uk/blog/introducing-the-unity.mvc3-nuget-package-to-reconcile-mvc3-unity-and-idisposable

Ok, I found the culprit. It has to do with how I was registering my database factory.

Instead of

container.RegisterType<IDatabaseFactory, DatabaseFactory>();

I needed

container.RegisterType<IDatabaseFactory, DatabaseFactory>(new HierarchicalLifetimeManager());

I found the information on this web site:

http://www.devtrends.co.uk/blog/introducing-the-unity.mvc3-nuget-package-to-reconcile-mvc3-unity-and-idisposable

自此以后,行同陌路 2024-12-09 20:37:27

这是工作单元的一个非常复杂的实现。我实际上更喜欢这个:

http://azurecoding.net/blogs/brownie/archive/2010/09/22/irepository-lt-t-gt-and-iunitofwork.aspx

很多更简单,也更灵活。尽管您确实必须自己解决一些问题。

That's an awfully complex implementation of Unit of Work. I actually prefer this one:

http://azurecoding.net/blogs/brownie/archive/2010/09/22/irepository-lt-t-gt-and-iunitofwork.aspx

Much simpler, and much more flexible. Although you do have to work out a few things for yourself.

泪意 2024-12-09 20:37:27

可能只是一个拼写错误,但在 UnitOfWork 中,您的私有 MyProjectContext 称为 dataContext (小写 d),

但在您的提交方法中,您调用 DataContext.Commit。有没有可能实际上调用了您不打算调用的静态方法?更有可能是一个错字,但我想我会指出来。

+1 表示 UnitOfWork 的实现过于复杂。

May just be a typo but in UnitOfWork your private MyProjectContext is called dataContext (lowercase d)

But in your commit method your calling DataContext.Commit. Any chance that's actually calling a static method that you didn't intend to call? More likely a typo but thought I'd point it out.

+1 for an overly complex implementation of UnitOfWork.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文