I always get messed using attach and detach methods. Is it possible to not use them?
我总是使用附加和分离方法搞砸了。有可能不使用它们吗?
If yes, then why people don't usually use this concept?
如果是,那么为什么人们通常不会使用这个概念呢?
I think there is some option with overriding SaveChanges()...
我认为有一些选项可以覆盖SaveChanges()......
1 个解决方案
#1
2
You are having to attach/detach the entity due to how you are using the context. If you use the context and manipulate the data while inside the context you will not have to worry about the attach/detach.
由于您使用上下文的方式,您必须附加/分离实体。如果您在上下文中使用上下文并操纵数据,则不必担心附加/分离。
using(var context = new DbContext())
{
var item = context.GetItem();
item.Name = "FooBar";
context.SaveChange();
}
So you will more then likely have to use attach/detach if you are using the repository pattern or any other method of data access where you are pulling the data out of the DAL and working with it in the Business layer or front end. You also do not have to manually detach the object, once you close the connection it becomes a detached object which before saving you just have to reattach to the context.
因此,如果您使用存储库模式或任何其他数据访问方法,您将更多地使用附加/分离,您将数据从DAL中提取出来并在业务层或前端中使用它。您也不必手动分离对象,一旦关闭连接它就成为一个分离的对象,在保存之前您只需重新连接到上下文。
Here is a repository I really like. It is getting a little bloated now but it gets you working with the data really quickly. I like their "lazy loading" of the dbsets, I myself have stripped out the abstraction of the ORM as I know I want to stick with Entity Framework.
这是我真正喜欢的存储库。它现在变得有点臃肿,但它可以让你快速处理数据。我喜欢他们对dbsets的“延迟加载”,我自己已经删除了ORM的抽象,因为我知道我想坚持使用Entity Framework。
Blog explaining the Repo: http://blog.longle.net/2013/05/11/genericizing-the-unit-of-work-pattern-repository-pattern-with-entity-framework-in-mvc/
博客解释回购:http://blog.longle.net/2013/05/11/genericizing-the-unit-of-work-pattern-repository-pattern-with-entity-framework-in-mvc/
Actual Repo site: https://genericunitofworkandrepositories.codeplex.com
实际回购网站:https://genericunitofworkandrepositories.codeplex.com
Here is a source directly from M$ that should give you a better idea of how/when to reattach.
这是一个直接来自M $的来源,可以让您更好地了解如何/何时重新附加。
http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an- ASP净MVC应用程序
Here is another link to help that deals directly with different cases and how to attach the entity. http://msdn.microsoft.com/en-us/data/jj592676.aspx
这是帮助直接处理不同案例以及如何附加实体的另一个链接。 http://msdn.microsoft.com/en-us/data/jj592676.aspx
#1
2
You are having to attach/detach the entity due to how you are using the context. If you use the context and manipulate the data while inside the context you will not have to worry about the attach/detach.
由于您使用上下文的方式,您必须附加/分离实体。如果您在上下文中使用上下文并操纵数据,则不必担心附加/分离。
using(var context = new DbContext())
{
var item = context.GetItem();
item.Name = "FooBar";
context.SaveChange();
}
So you will more then likely have to use attach/detach if you are using the repository pattern or any other method of data access where you are pulling the data out of the DAL and working with it in the Business layer or front end. You also do not have to manually detach the object, once you close the connection it becomes a detached object which before saving you just have to reattach to the context.
因此,如果您使用存储库模式或任何其他数据访问方法,您将更多地使用附加/分离,您将数据从DAL中提取出来并在业务层或前端中使用它。您也不必手动分离对象,一旦关闭连接它就成为一个分离的对象,在保存之前您只需重新连接到上下文。
Here is a repository I really like. It is getting a little bloated now but it gets you working with the data really quickly. I like their "lazy loading" of the dbsets, I myself have stripped out the abstraction of the ORM as I know I want to stick with Entity Framework.
这是我真正喜欢的存储库。它现在变得有点臃肿,但它可以让你快速处理数据。我喜欢他们对dbsets的“延迟加载”,我自己已经删除了ORM的抽象,因为我知道我想坚持使用Entity Framework。
Blog explaining the Repo: http://blog.longle.net/2013/05/11/genericizing-the-unit-of-work-pattern-repository-pattern-with-entity-framework-in-mvc/
博客解释回购:http://blog.longle.net/2013/05/11/genericizing-the-unit-of-work-pattern-repository-pattern-with-entity-framework-in-mvc/
Actual Repo site: https://genericunitofworkandrepositories.codeplex.com
实际回购网站:https://genericunitofworkandrepositories.codeplex.com
Here is a source directly from M$ that should give you a better idea of how/when to reattach.
这是一个直接来自M $的来源,可以让您更好地了解如何/何时重新附加。
http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an- ASP净MVC应用程序
Here is another link to help that deals directly with different cases and how to attach the entity. http://msdn.microsoft.com/en-us/data/jj592676.aspx
这是帮助直接处理不同案例以及如何附加实体的另一个链接。 http://msdn.microsoft.com/en-us/data/jj592676.aspx