EF 4.1 SQL Compact字节

时间:2011-06-10 21:14:51

标签: c# .net entity-framework entity-framework-4 sql-server-ce-4

我的查询中存在一些性能瓶颈。每当我在我的实体中将一个属性作为一个字节引入时,会发生什么,EF 4.1在使用它之前将其转换为int。给出的代码将解释:

var segmentQuery = workUnit.SegmentRepository.GetQuery()
                                             .Where(x => x.FileId == file.Id)
                                             .Where(x => x.StateValue == (byte)SegmentState.Unhandeled)
                                             .OrderBy(x => x.Index);

很好地翻译成:

SELECT 
....
FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
    ...
    [Extent1].[StateValue] AS [StateValue]
    FROM [Segments] AS [Extent1]
    WHERE ([Extent1].[FileId] = @p__linq__0) AND (0 = [Extent1].[StateValue])
)  AS [Project1]
ORDER BY [Project1].[Index] ASC

但是,在上面的情况中:StateValue实际上是一个整数,这对我的要求(4种不同的状态)来说很多,但是当把它改成一个字节时,我得到:

SELECT ...
FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
      ...
    [Extent1].[StateValue] AS [StateValue]
    FROM [Segments] AS [Extent1]
    WHERE ([Extent1].[FileId] = @p__linq__0) AND (0 = ( CAST( [Extent1].[StateValue] AS int)))
)  AS [Project1]
ORDER BY [Project1].[Index] ASC

由于这个表一天可能包含超过10万行,因此其状态字段的空间效率(虽然幸运不是必需的)仅占用1个字节,但是,更改为字节会导致查询失败。

我做错了什么吗?有什么我可以做的吗?这个“问题”是否已知?

谢谢!

**更新**

[Flags]
public enum SegmentState : byte
{
    Unhandeled,
    Downloaded,
    Invalid,
    Assembled
}

在我的实体中:

/// <summary>
/// Dont use this, use SegmentState instead
/// </summary>
[Required]
public byte StateValue
{
    get { return _stateValue; }
    set { _stateValue = value; }
}

public SegmentState State
{
    get { return (SegmentState)StateValue; }
    set 
    {
        if (State != value)
        {
            StateValue = (byte)value;
            RaisePropertyChanged(StatePropertyName);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

严重的是不要使用EF - 这将是你生命中最大的痛苦。 看看C#中的MASSIVE和Dynamics - 它会睁开你的眼睛; - )