Entity Framework: saving objects without saving to the database

How to store a temporary item in an ObjectContext without saving to the database?

Saving context 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 blank 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 am I mistaken?

+6
source share
3 answers

Is it correct that the second page is the second request?

In this case, you will have a new collection of HttpContext.Items, and your values โ€‹โ€‹from the last request will disappear. Try using a session to store these values โ€‹โ€‹in this case.

Footnote: EntityContext should be used for only one request and can be stored in the HttpContext.Items collection for this reason, but not as a session value! Keep only results here, such as score.

+3
source

This is the wrong approach, HttpContext has only the scope of one HTTP request, so in the second request you are dealing with a different context.

But even if it were possible to save the database context in this way, i.e. even if you decide to keep it in a session - this is not the way to go - the area of โ€‹โ€‹each context should be a single unit of work, you should not save it for a long period of time, especially in a web environment.

Just save your temporary items in the session directly and create a new context to load these items when you are ready.

+2
source

To fulfill the query for new data using EF, you will need to save. You can list and then run a query on the list, but this will require you to save the list in some kind of static memory (session state, browsing, cache), but if the list is large, this can create other problems.

You can do everything in TRANSACTION . Transferring a transaction until you complete or roll back. Entity objects are saved, but when the transaction is rolled back, any changes are discarded. I think the transaction will persist through callbacks and redirects, but should be committed or deleted by the time your page is displayed.

0
source

Source: https://habr.com/ru/post/887719/


All Articles