Linq 异常“此提供程序只支持对返回实体或投影(包含所有标识列)的有序查询使用 Skip()...”

问题:asp.net使用linq,在sql server 2005下面使用视图分页没有问题,但在sql server 2000下面使用视图
提示:此提供程序只支持对返回实体或投影(包含所有标识列)的有序查询使用 Skip(),这种查询为单表(非联接)查询,或者为 Distinct、Except、Intersect 或 Union (非 Concat)操作。

原因:LINQ生成分布查询语句时,需要依赖主键。那么,如下情况将导致出现上述错误
1)数据表未定义主键
2)查询对象为视图对象

奇怪的是:在sql server 2005里,能识别视图中某个表的主键,不会提示错误,到sql server 2000下面就提示错误。

反正也不能回避问题,再网上搜索了一下相关的问题,确实很多人遇到,主要是试图的主键问题,表的主键很容易解决,在sql server里视图是没有主键的。
网上的一种解决方案能解决这个问题:更改LINQ to SQL 类。
找到LINQ to SQL 类的文件,下面有个xxx.designer.cs,找到相应的试图:
Code
右键转到定义:会有这个对象的定义
[ Table(Name="dbo.Product_Class_View") ]
public  partial class Product_Class_View
{
    
    private 
int  _ProID;
    
    private string _ProName;
    
    private System.Nullable
< int >  _CateID;
    
    private System.Nullable
< int >  _ClassID;
    
    private string _ImgUrl;
    
    private string _Summary;
    
    private string _Content;
    
    private System.Nullable
< int >  _OrdNum;
    
    private System.Nullable
< int >  _ViewNum;
    
    private System.Nullable
< int >  _InfoVer;
    
    private System.Nullable
< System. DateTime >  _CreateDate;
    
    private string _ClassName;
    
    
public  Product_Class_View()
    {
    }
    
    
[ Column(Storage="_ProID", DbType="Int NOT NULL", IsDbGenerated=true,IsPrimaryKey=true) ]
    
public   int  ProID
    {
        get
        {
            
return  this._ProID;
        }
        
set
        {
            
if  ((this._ProID  !=  value))
            {
                this._ProID 
=  value;
            }
        }
    }
    
    
[ Column(Storage="_ProName", DbType="NVarChar(255)") ]
    
public  string ProName
    {
        get
        {
            
return  this._ProName;
        }
        
set
        {
            
if  ((this._ProName  !=  value))
            {
                this._ProName 
=  value;
            }
        }
    }
    
    
[ Column(Storage="_CateID", DbType="Int") ]
    
public  System.Nullable < int >  CateID
    {
        get
        {
            
return  this._CateID;
        }
        
set
        {
            
if  ((this._CateID  !=  value))
            {
                this._CateID 
=  value;
            }
        }
    }
    

关键是这一句: [Column(Storage="_ProID", DbType="Int NOT NULL", IsDbGenerated=true, IsPrimaryKey=true)]
加上加粗部分的就可以了。

你可能感兴趣的:(LINQ)