Rowversion comparison in Entity Framework

前端 未结 3 2133
温柔的废话
温柔的废话 2021-02-14 22:31

How should I compare rowversion fields using Entity Framework? I have one table which has a rowversion column, I want to get data from tables for which

相关标签:
3条回答
  • 2021-02-14 23:02

    Use EntitySQL.

    // Drop down to ObjectContext and query against ObjectSet:
    var objectContext = (dbContext as IObjectContextAdapter).ObjectContext;
    // Create param for EntitySQL
    var param = new ObjectParameter("rowVersion", rowVersion);
    // Create IQueryable<T>
    var query = objectContext.CreateObjectSet<T>.Where("it.RowVersion > @rowVersion",param);
    
    0 讨论(0)
  • 2021-02-14 23:06

    Here is what we did to solve this. Use a compare extension like this:

    public static class EntityFrameworkHelper
    {
        public static int Compare(this byte[] b1, byte[] b2)
        {
            throw new Exception("This method can only be used in EF LINQ Context");
        }
    }
    

    Then you can do this:

    byte[] rowversion = .....somevalue;
    _context.Set<T>().Where(item => item.RowVersion.Compare(rowversion) > 0);
    

    The reason this works without a C# implementation is because the Compare extension method is never actually called, and EF LINQ simplifies x.Compare(y) > 0 down to x > y.

    0 讨论(0)
  • 2021-02-14 23:06

    Are you sure that is Kosher use of RowVersion ? What happens when it rolls over ?

    I think you will need to convert it to string or int first. eg

    Encoding.Unicode.GetString(ba)
    

    You can also call SPs from EF. :-)

    0 讨论(0)
提交回复
热议问题