Requesting an Entity Structure for Values ​​Just Added But Not Saved

I have been using Entity Framework after a couple of years, and now I have a little problem.

I add an object to my table,

Entities.dbContext.MyTable.Add(obj1);

and here is normal.

Then I would like to make a request in MyTable, for example

Entities.dbContext.MyTable.Where(.....)

The above code will query my MyTable in db.

Is there a way to request the value just added before saveChanges ? (obj1) How?

UPDATE

Why do I need it? Since for each new element I add, I need to edit some values ​​in the previous and next records (there is a datetime field in this table)

UPDATE2

, , saveChanges . , , datetime . . : , , , "Obj1", , , . ?

+4
2

dbContext :

 var addedEntities = dbContext.ChangeTracker.Entries()
   .Where(x => x.State == EntityState.Added && x.Entity.GetType().Name == "MyTable")
   .Select(x => x.Entity as MyTable);

,

dbContext.MyTable.Where(x => -criteria-).ToList().AddRange(addedEntities);

+6

, . , EF 6, . =)

UPDATE2

public void BulkInsertObj(List<TEntity> objList)
{
    using (var context = new dbContext()) 
    { 
        using (var dbContextTransaction = context.Database.BeginTransaction()) 
        {  
            try 
            { 
                foreach(var obj1 in objList)
                {
                    dbContext.MyTable.Add(obj1);

                    //obj1 should be on the context now 
                    var previousEntity = dbContext.MyTable.Where(.....) //However you determine this
                    previousEntity.field = something

                    var nextEntity = dbContext.MyTable.Where(.....) //However you determine this
                    nextEntity.field = somethingElse
                }

                context.SaveChanges(); 
                dbContextTransaction.Commit(); 
            } 
            catch (Exception) 
            { 
                dbContextTransaction.Rollback(); 
            } 
        } 
    } 
}

MSDN EF6

0

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


All Articles