如何使用实体框架比较rowversion字段?我有一个具有rowversion列的表,我想从行版本高于指定值的表中获取数据.
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 the row version is higher than specified value.
byte[] rowversion = ... some value; _context.Set<T>().Where(item => item.RowVersion > rowVersion);此行不起作用,它会引发错误:
This line does not work, it throws the error:
运算符'>'不能应用于类型为'byte []'和'byte []'的操作数
有什么想法可以比较C#/Entity Framework中的rowversion字段吗?
Any idea how I can compare rowversion fields in C#/Entity Framework?
推荐答案以下是我们为解决此问题所做的工作.使用这样的比较扩展名:
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);之所以没有C#实现的原因是,实际上从未调用过Compare扩展方法,并且EF LINQ将x.Compare(y) > 0简化为x > y.
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.
更多推荐
实体框架中的行版本比较
发布评论