Optimistic Concurrency with Entity Framework and ORACLE

.net c# entity-framework entity-framework-6 oracle

Question

Trying to implement Optimistic Concurrency with Entity Framework 6 (Designer First approach) and Oracle database (ver 12).

SQL Server has RowVersion attribute which can be used to track the changes, couldn't find any similar attribute in Oracle.

Was able to use a Trigger to simulate the functionality of RowVersion, just wondering whether there are any other alternatives instead of creating a Trigger for the sole purpose of versioning.

Thanks

1
1
1/27/2017 8:24:51 PM

Accepted Answer

Sure, there is a way of doing it without trigger. Doing it without a trigger will work with any database supported by Entity Framework providers...

Unfortunately, I am not sure about Designer First approach but with Code First you can do it like this(it's probably similar with Designer First, maybe using partial class). Looking forward, if possible, you should use Code First, because it's only way supported by new Entity Framework Core.

Model:

public abstract class ConcurrencyTracker
{
    [ConcurrencyCheck]
    public long LastChange { get; set; }

    [NotMapped]
    public DateTime LastChangeTime
    {
        set
        {
            long timestamp = value.Ticks - new DateTime(1970, 1, 1).Ticks;
            timestamp = timestamp / TimeSpan.TicksPerSecond;
            LastChange = timestamp;
        }
    }
}

public class Product : ConcurrencyTracker
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Context:

public override int SaveChanges()
{
    var currentTime = DateTime.Now;

    var changed = ChangeTracker.Entries<ConcurrencyTracker>().Where(c => c.State != EntityState.Unchanged);
    if (changed != null)
    {
        foreach (var item in changed)
        {
            item.Entity.LastChangeTime = currentTime;
        }
    }
    return base.SaveChanges();
}

EF will include LastChange column in all updates of Product model so there is optimistic concurrency.

2
1/29/2017 4:39:01 PM

Popular Answer

You can use the ORA_ROWSCN pseudo column in Oracle rather than creating and maintaining a timestamp. You will need to create the tables with ROWDEPENDENCIES enabled



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow