简体   繁体   English

在ASP.NET MVC应用程序中使用EntityFramework的持久性

[英]Persistence with EntityFramework in ASP.NET MVC application

In my ASP.NET MVC application I need to implement persistence of data. 在我的ASP.NET MVC应用程序中,我需要实现数据的持久性。 I've choose Entity Framework for its ability to create classes, database tables and queries from entity model so that I don't have to write SQL table creation or Linq to SQL queries by hand. 我选择Entity Framework是因为它具有从实体模型创建类,数据库表和查询​​的能力,因此不必手动编写SQL表创建或Linq到SQL查询。 So simplicity is my goal . 因此, 简单是我的目标

My approach was to create model and than a custom HttpModule that gets called at the and of each request and that just called SaveChanges() on the context. 我的方法是创建模型,而不是创建一个自定义HttpModule ,该自定义HttpModule在和的每个请求中被调用,而在上下文中仅被调用SaveChanges() That made my life very hard - entity framework kept throwing very strange exception. 这使我的生活变得非常艰难-实体框架不断抛出非常奇怪的异常。 Sometimes it worked - no exception but sometimes it did not. 有时它起作用了-也不例外,但有时却没有。 First I was trying to fix the problems one by one but when I got another one I realized that my general approach is probably wrong. 首先,我试图一个接一个地解决问题,但是当我遇到另一个问题时,我意识到我的一般方法可能是错误的。

So that is the general practice to implement for implementing persistence in ASP.NET MVC application ? 那是在ASP.NET MVC应用程序中实现持久性实现的一般惯例吗? Do I just call saveChanges after each change ? 每次更改后,我只调用saveChanges吗? Isn't that little inefficient ? 那不是效率低下吗? And I don't know how to do that with Services patter anyway (services work with entities so I'd have to pass context instance to them so that they could save changes if they make some). 而且我仍然不知道如何使用服务模式来做到这一点(服务与实体一起工作,因此我必须将上下文实例传递给它们,以便它们进行更改时可以保存更改)。

Some links to study materials or tutorials are also appreciated. 还提供了一些学习资料或教程的链接。


Note: this question asks for programing practice. 注意:这个问题要求编程实践。 I ask those who will consider it vague to bear in mind that it is still solving my very particular problem and right technique will save me a lot of technical problems before voting to close. 我要求那些认为模糊的人记住,它仍在解决我非常特殊的问题,而正确的技术将在投票结束之前为我节省很多技术问题。

It's going to be no more or less efficient than calling a stored procedure that many number of times (with respect to number of connections that need to be made). 它要比多次调用存储过程(就需要建立的连接数而言)效率更高或更低。

Nominally, you would make all your changes to the object set, then SaveChanges to commit all those changes. 名义上,您将对对象集进行所有更改,然后单击SaveChanges提交所有这些更改。

So instead of doing this: 所以不要这样做:

mySet.Objects.Add(someObject);
mySet.SaveChanges();
mySet.OtherObjects.Add(someOtherObject);
mySet.SaveChanges();

You just need to do: 您只需要做:

mySet.Objects.Add(someObject);
mySet.OtherObjects.Add(someOtherObject);
mySet.SaveChanges();
// Commits Both Changes

You just need to make sure SaveChanges gets called before your request finishes. 您只需要确保在请求完成之前调用SaveChanges At the bottom of a controller action is an ideal place. 控制器动作的底部是理想的位置。 My controller actions typically look like this: 我的控制器动作通常如下所示:

public ActionResult SomeAction(...) 
{
    _repository.DoSomething();
    ...
    _repository.DoSomethingElse();
    ...
    _repository.SaveChanges();
    return View(...);
}

This has the added benefit that if an exception gets thrown, then SaveChanges will not get called. 这样做的好处是,如果引发异常,则不会调用SaveChanges。 And you can either handle the exception in the action or in Controller.OnException . 您可以在操作中或Controller.OnException处理异常。

Usually your data access is wrapped by an object implementing the repsitory pattern. 通常,您的数据访问由实现回收模式的对象包装。 You then invoke a Save() method on the repository. 然后,您在存储库上调用Save()方法。

Something like 就像是

var customer = customerRepository.Get(id);
customer.FirstName = firstName;
customer.LastName = lastName;
customerRepository.SaveChanges();

The repository can then be wrapped by a service layer to provide view model objects or DTO's 然后,存储库可以由服务层包装以提供视图模型对象或DTO。

Isn't that little inefficient ? 那不是效率低下吗?

Don't prematurely optimise. 不要过早乐观。 When you have a performance issue, analyse the performance, identify a cause and then optimise. 当您遇到性能问题时,请分析性能,找出原因,然后进行优化。 Repeat. 重复。

Update 更新

A repository wraps data access, usually a single entity. 存储库包装数据访问权,通常是单个实体。 A service layer wraps business logic and can access multiply entities through multiple repositories. 服务层包装业务逻辑,并可以通过多个存储库访问多个实体。 It usually deals with 'slim' models or DTO's. 它通常处理“苗条”模型或DTO。

An example could be something like getting a list of invoices for a customer 例如,例如获取客户发票清单之类的东西

public Customer GetCustomerWithInvoices(int id) {

  var customer = customerRepository.Get(id);
  var invoiceList = invoiceRepository.GetAllInvoicesFor(id);

  return new Customer {
    Customer = customer,
    Invoices = invoiceList
  };

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM