SharePoint中使用Linq出现未将对象引用到实例化的解决方法

今天在使用Sharepoint的Linq是出现为未将对象引用实例化的问题。在网上一下,发现许多朋友也出现相同的问题。

问题代码

using (DataContext ctx = new DataContext(portalUrl))
{
EntityList<CustomConfiguration> constConfig = ctx.GetList<EC.Entities.ConstantsConfiguration>("Custom Configuration");
IList<CustomConfiguration> list=constConfig.ToList();
}

 

 

程序运行到ToList()的时候就会报错:

Category: SharePoint Guidance: An exception has occurred.   ExceptionType: 'NullReferenceException'   ExceptionMessage: 'Object reference not set to an instance of an object.'   StackTrace: ' 
at Microsoft.SharePoint.Linq.SPItemMappingInfo.GetId(SPListItem item, String listName)     
at Microsoft.SharePoint.Linq.SPItemMappingInfo.MaterializeEntity[TEntity](DataContext dc, SPDataList list, SPListItem item, SPItemMappingInfo itemMappingInfo, JoinPath joinPath) …

 

检查发现将DataContext.ObjectTrackingEnabled设为False,再使用ToList()程序就可以通过了。

正确代码

using (DataContext ctx = new DataContext(portalUrl))
{
    ctx.ObjectTrackingEnabled = false;
    EntityList<CustomConfiguration> constConfig = ctx.GetList<EC.Entities.ConstantsConfiguration>("Custom Configuration");
    IList<CustomConfiguration> list=constConfig.ToList();
}

  

 

SDK中关于ObjectTrackingEnabled属性的描述:

Default: true

gets or sets a value that indicates whether changes in objects are tracked. If this property is false ,changes cannot be persisted to the content database .

If your code only queries lists, rather than adding, deleting, or editing list items, then you can turn off object change tracking. Doing so will improve performance. Set the ObjectTrackingEnabled property to false.

 


 

 

转载于:https://www.cnblogs.com/snailJuan/archive/2012/04/28/2475511.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值