实体框架中的Rowversion比较

bha*_*lad 9 entity-framework rowversion c#-4.0

我应该如何rowversion使用Entity Framework 比较字段?我有一个表有一个rowversion列,我想从表中获取行版本高于指定值的数据.

byte[] rowversion = ... some value;
 _context.Set<T>().Where(item => item.RowVersion > rowVersion);
Run Code Online (Sandbox Code Playgroud)

这行不起作用,它抛出错误:

不能应用于'byte []'和'byte []'类型的操作数

知道如何比较rowversionc#/ EF中的字段吗?

小智 6

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

Then you can do this:

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

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.


phi*_*ady 1

您确定 RowVersion 的使用符合犹太洁食规范吗?当它翻过来时会发生什么?

我认为您需要首先将其转换为字符串或整数。例如

Encoding.Unicode.GetString(ba)
Run Code Online (Sandbox Code Playgroud)

您还可以从 EF 呼叫 SP。:-)

  • 即使您可以连续为该表维持每秒 100 万次插入/更新,如果我的数学正确的话,它也会在大约 584000 年后滚动。 (2认同)