【发布时间】:2012-03-22 01:37:04
【问题描述】:
下面是我使用 LINQ to SQL 的代码,在执行时我收到一条错误消息,提示“对象引用未设置为对象的实例”。我加入了 3 个表用户、用户角色和角色。用户角色是桥表.我使用连接从使用桥接表的 2 个表中检索数据,我得到对象引用错误。
public class Users : CollectionFactoryBase
{
public Users()
{
this.Summary = "Collection of Users";
}
public override Collection MakeCollection(CollectionRequestContext context)
{
UsersDataContext m_dataContext = new UsersDataContext();
const int maxItems_c = 150;
try
{
// string sessionvalue = HttpContext.Current.Session["SessionKey"] as string;
var Users = from p in m_dataContext.aspnet_Users
join t in m_dataContext.aspnet_UsersInRoles on
p.UserId equals t.UserId
join r in m_dataContext.aspnet_Roles on
t.RoleId equals r.RoleId
select new
{
UserName = p.UserName,
UserId = p.UserId,
RoleId = r.RoleId,
RoleName = r.RoleName,
userid = t.UserId,
roleid = t.RoleId
};
Collection collection = new Collection();
collection.Name = "Users";
foreach (var user in Users.Take(maxItems_c) )
{
collection.AddItem(user.UserName, user.RoleName, null, null, null, null, null);
}
return collection;
}
catch (Exception ex)
{
return ErrorCollection.FromException(ex);
}
}
}
}
【问题讨论】:
-
哪一行导致错误?
-
在构建时没有错误,在执行时它打开时出现此错误但没有特别显示行
-
在抛出异常时让 Visual Studio 中断,一分钟后就会清楚导致异常的原因。我敢打赌,您的
Collection课程不符合犹太教规。那到底是什么样的弱类型类? .net 通用集合不是更好吗?
标签: sql-server-2008 entity-framework-4