我正在使用Linq to SQL类来管理数据,并使用以下代码将数据绑定到Windows窗体中的dataGridview。当我尝试从左向右滚动datagrdview时抛出异常。
这里涉及的两个表是:
billing_UserRole
RoleId PK
ROLENAME
CreatedTS
UpdatedTS
billing_User
UserId PK //这也在其他一些表中引用。
RoleId FK
用户名
密码
姓
名字
CreatedTS
UpdatedTS
创建表格查询如下
CREATE TABLE [dbo].[billing_UserRole](
bk_UserRoleId BIGINT IDENTITY(100,1) NOT NULL,
bk_UserRoleName NVARCHAR(128) NOT NULL,
bk_CreatedTS DATETIME NULL,
bk_UpdatedTS DATETIME NULL,
CONSTRAINT [PK_billingUserRole] PRIMARY KEY CLUSTERED
(
[bk_UserRoleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
/* TABLE NAME : billing_User */
CREATE TABLE [dbo].[billing_User](
bk_UserId BIGINT IDENTITY(1,1) NOT NULL,
bk_Username NVARCHAR(256) NOT NULL,
bk_Password NVARCHAR(2048) NOT NULL DEFAULT('none'),
bk_FirstName NVARCHAR(256) NOT NULL,
bk_LastName NVARCHAR(256) NOT NULL,
bk_MobileNo NVARCHAR(128) NOT NULL,
bk_Address NVARCHAR(2048) NULL DEFAULT('none'),
bk_Remarks NVARCHAR(2048) NULL DEFAULT('none'),
bk_UserRoleId BIGINT NOT NULL,
bk_CreatedTS DATETIME NULL,
bk_UpdatedTS DATETIME NULL,
CONSTRAINT [PK_billingUser] PRIMARY KEY CLUSTERED
(
[bk_UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [FK_billingUser_billingUserRole] FOREIGN KEY(bk_UserRoleId) REFERENCES [dbo].[billing_UserRole](bk_UserRoleId)
)
表单中的代码,用于在form_load上绑定datagridview
private void UserManagementForm_Load(object sender, EventArgs e)
{
try
{
using (BillingKioskDataLayerDataContext dc = new BillingKioskDataLayerDataContext())
{
Table<billing_User> UserTable = dc.GetTable<billing_User>();
var UserList = UserTable.Select(o=>o).ToList();
billing_UserBindingSource.DataSource = UserList;
dataGridView1.DataSource = billing_UserBindingSource;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
这是抛出异常的代码。这是一个自动生成的代码。为什么会这样? 当我从左向右滚动DataGridView以查看剩余字段时,我才会收到此异常。
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="billing_UserRole_billing_User", Storage="_billing_UserRole", ThisKey="bk_UserRoleId", OtherKey="bk_UserRoleId", IsForeignKey=true)]
public billing_UserRole billing_UserRole
{
get
{
return this._billing_UserRole.Entity; **// I get DisposedObjectException :Cannot access a disposed object here**
}
set
{
billing_UserRole previousValue = this._billing_UserRole.Entity;
if (((previousValue != value)
|| (this._billing_UserRole.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._billing_UserRole.Entity = null;
previousValue.billing_Users.Remove(this);
}
this._billing_UserRole.Entity = value;
if ((value != null))
{
value.billing_Users.Add(this);
this._bk_UserRoleId = value.bk_UserRoleId;
}
else
{
this._bk_UserRoleId = default(long);
}
this.SendPropertyChanged("billing_UserRole");
}
}
}
堆栈跟踪:
at System.Data.Linq.DataContext.GetTable(Type type)
at System.Data.Linq.CommonDataServices.GetDataMemberQuery(MetaDataMember member, Expression[] keyValues)
at System.Data.Linq.CommonDataServices.DeferredSourceFactory`1.ExecuteKeyQuery(Object[] keyValues)
at System.Data.Linq.CommonDataServices.DeferredSourceFactory`1.Execute(Object instance)
at System.Data.Linq.CommonDataServices.DeferredSourceFactory`1.DeferredSource.GetEnumerator()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Data.Linq.EntityRef`1.get_Entity()
at BillingKiosk.billing_User.get_billing_UserRole() in e:\XGHProjects\BillingKiosk\BillingKiosk\BillingKioskDataLayer.designer.cs:line 663
答案 0 :(得分:0)
您很可能在相关实体中遇到Lazy或Deferred Loading of columns。 您可以使用DataLoadOptions.LoadWith方法来包含相关列。
http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.loadwith(v=vs.110).aspx
顺便说一下:如果确实如此,很容易建立:删除datacontext上的Using()。 [仅用于诊断...]。如果它运作良好,懒惰加载...