实体框架:存储实体而不保存到数据库
如何在ObjectContext中存储临时项而不保存到数据库?
Context 存储在 HttpContext 中,按类提供:
public static class HttpContextExtension
{
public static MyEntityDataModelContainer GetMyContext(this HttpContext httpContext)
{
if (httpContext.Items["MyEntityDataModelContainer"] == null)
{
httpContext.Items.Add("MyEntityDataModelContainer", new MyEntityDataModelContainer());
}
return (MyEntityDataModelContainer)httpContext.Items["MyEntityDataModelContainer"];
}
}
有两个空页面: 1)FirstPage.aspx.cs:
public class FirstPage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// crete new item
MyEntity newTemporaryItem = new MyEntity { MyEntityID = Guid.NewGuid() };
// attach them to Context
HttpContext.Current.GetMyContext().MyEntitySet.Attach(newTemporaryItem);
// save changes
HttpContext.Current.GetMyContext().SaveChanges();
// get all attached to Context items
var addedItems = (from se in HttpContext.Current.GetMyContext().ObjectStateManager.GetObjectStateEntries(EntityState.Unchanged)
where se.Entity is MyEntity
select se.Entity).AsQueryable();
int CountInFirstPage = addedItems.Count();
}
}
所以,CountInFirstPage = 1。
2)SecondPage.aspx.cs:
public class FirstPage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// get added in First page items From HttpContext
var addedItems = (from se in HttpContext.Current.GetMyContext().ObjectStateManager.GetObjectStateEntries(EntityState.Unchanged)
where se.Entity is MyEntity
select se.Entity).AsQueryable();
int CountInSecondPage = addedItems.Count();
}
}
这里CountInSecondPage = 0。
我哪里错了?
How to store temporary item in ObjectContext without saving to database?
Context storing in HttpContext, providing by class:
public static class HttpContextExtension
{
public static MyEntityDataModelContainer GetMyContext(this HttpContext httpContext)
{
if (httpContext.Items["MyEntityDataModelContainer"] == null)
{
httpContext.Items.Add("MyEntityDataModelContainer", new MyEntityDataModelContainer());
}
return (MyEntityDataModelContainer)httpContext.Items["MyEntityDataModelContainer"];
}
}
There are two empty pages:
1) FirstPage.aspx.cs:
public class FirstPage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// crete new item
MyEntity newTemporaryItem = new MyEntity { MyEntityID = Guid.NewGuid() };
// attach them to Context
HttpContext.Current.GetMyContext().MyEntitySet.Attach(newTemporaryItem);
// save changes
HttpContext.Current.GetMyContext().SaveChanges();
// get all attached to Context items
var addedItems = (from se in HttpContext.Current.GetMyContext().ObjectStateManager.GetObjectStateEntries(EntityState.Unchanged)
where se.Entity is MyEntity
select se.Entity).AsQueryable();
int CountInFirstPage = addedItems.Count();
}
}
So, CountInFirstPage = 1.
2) SecondPage.aspx.cs:
public class FirstPage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// get added in First page items From HttpContext
var addedItems = (from se in HttpContext.Current.GetMyContext().ObjectStateManager.GetObjectStateEntries(EntityState.Unchanged)
where se.Entity is MyEntity
select se.Entity).AsQueryable();
int CountInSecondPage = addedItems.Count();
}
}
Here CountInSecondPage = 0.
Where I'm wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我说得对吗,第二页是第二个请求?
在这种情况下,您将拥有一个新的 HttpContext.Items 集合,并且上次请求中的值将消失。在这种情况下,请考虑使用会话来存储这些值。
脚注:
EntityContext 只能用于一个请求,因此可以存储在 HttpContext.Items 集合中,但绝不能作为 Session 值!此处仅存储结果(例如计数)。
Am I right that the second page is a second request?
In that case you have a new HttpContext.Items collection and your values from the last request are gone. Consider to use a session to store these values in such a case.
Footnote:
The EntityContext should only be used for one request and can be stored in the HttpContext.Items collection for that reason but never as a Session value! Store just results here like the count.
这是错误的方法,
HttpContext
仅具有单个 HTTP 请求的范围,因此您在第二个请求中处理不同的上下文。但是,即使可以以这种方式存储数据库上下文,即即使您决定将其存储在会话中 - 这不是可行的方法 - 每个上下文的范围应该是单个工作单元,您不应该使其长时间保持活动状态,尤其是在 Web 环境中。
只需将临时项目直接保存在会话中,并在准备好时创建一个新的上下文来上传这些项目。
This is the wrong approach,
HttpContext
only has a scope of a single HTTP request, so you are dealing with a different context in the second request.But even if it were possible to store the DB context that way, i.e. even if you decided to store it in the Session - this is not the way to go - the scope of each context should be a single unit of work, you should not keep it alive for an extended period of time, especially in a Web environment.
Just save your temporary items in the Session directly, and create a new context to upload these items when you are ready to.
为了使用 EF 对新数据运行查询,您需要保存。您可以创建一个列表,然后针对该列表运行查询,但这需要您将列表保留在某种静态内存中(会话状态、视图状态、缓存),但如果列表很大,可能会产生其他问题。
您可以在事务中完成所有操作。传递事务直到您提交或回滚。实体对象被保存,但当事务回滚时,任何更改都将被撤消。我认为事务将通过回发和重定向持续存在,但需要在页面呈现时提交或处置。
In order to run a query on your new data using EF you will need to save. You could to a to list then run the query against the list but that will require you to keep the list in some sort of static memory (session state, viewstate, cache) but if the list is large that may create other problems.
You could do everything in a TRANSACTION. Passing the transaction around until you either commit or roll back. The entity objects get saved but when the transaction is rolled back then any changes are undone. I think that the transaction will persist through postbacks and redirects but will need to be committed or disposed by the time your page is rendered.