实体框架中的行版本比较 [英] Rowversion comparison in Entity Framework

查看:87
本文介绍了实体框架中的行版本比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用实体框架比较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.

这篇关于实体框架中的行版本比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆