Entity Framework: Storing Entities without saving to Database
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
{
pro开发者_如何转开发tected 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?
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.
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.
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.
精彩评论