0

I have for example database with codes entities and I want in single transaction read and update some information.

What will be marked and "locked" or "dirty" when I execute such statement from Entity Framework 6.

using(var context = new codesEntities())
{
    using (var trans = db.Database.BeginTransaction(System.Data.IsolationLevel.Snapshot))
    {
        var codes_in_group = context.codes.Where(x => x.group == 1);        
    }
}

I am not sure but maybe nothing since nothing is read from querable. Is it anything marked as read?

What will be marked as read if execute such code?

using(var context = new codesEntities())
{
    using (var trans = db.Database.BeginTransaction(System.Data.IsolationLevel.Snapshot))
    {
        var codes_in_group = context.codes.Where(x => x.group == 1).ToList();       
    }
}

Or such code:

using(var context = new codesEntities())
{
    using (var trans = db.Database.BeginTransaction(System.Data.IsolationLevel.Snapshot))
    {
        var codes_in_group = context.codes.Where(x => x.group == 1).GetEnumerator();        
    }
}
Chameleon
  • 9,722
  • 16
  • 65
  • 127
  • I do not want solve problem but I am not understand how Entity Frameworks work exactly. I want write consistent transaction and avoid collisions. I can ask about counting of eggs, cats, and so on as problem but problem is what will be marked and dirty read in this transaction. – Chameleon Feb 06 '18 at 10:56
  • I assume that I will read (a, b, c) and try to write (b, c, d) in parallel with many other processes - problem is that if I lock whole table the chance of collision will be near 100% if lock 1,2 rows from table chance will be 1% so I am not sure what will happen. For example I can use enumerator to read row by row and read instead reading whole list to reduce collision chance if it works like that. – Chameleon Feb 06 '18 at 11:01
  • This is not lock table at all I think (not sure) `context.codes.Where(x => x.group == 1)`. This will lock on row `context.codes.Where(x => x.group == 1).Single()` for sure. Not sure what will do `context.codes.Where(x => x.group == 1).toList()` - I think it will lock whole table (not lock really - dirty reads rather if snapshot)? – Chameleon Feb 06 '18 at 11:04
  • That is true lock on read not happen on snapshot but instead of it there is dirty reads? For example if I do `i += 1` I need to be sure if `i` is not modified before I commit transaction so I think there will be dirty reads. – Chameleon Feb 06 '18 at 11:20
  • Possible duplicate of [Read committed Snapshot VS Snapshot Isolation Level](https://stackoverflow.com/questions/2741016/read-committed-snapshot-vs-snapshot-isolation-level) – mjwills Feb 06 '18 at 11:25
  • I will read your suggestion today maybe it is helpful looks promising thank you for help I will respond as I read it. – Chameleon Feb 06 '18 at 11:36
  • I read article it not help directly but I get idea to use SQL Query Editor to test transaction behavior and I learns me how it works. Now I designed algorithm and doing unit tests. Thank you for help. I think it is not duplicate but need read article before I decide. – Chameleon Feb 07 '18 at 11:24

0 Answers0