WPF DataGrid 无法关闭 DB (Matisse) 连接
我使用 Matisse DB 与 .NET 绑定从数据库中获取对象并填充 WPF 数据网格。数据库为我生成了一个 LINQ 上下文,我可以使用它从数据库中检索对象。我试图简单地将对象的内容绑定到 WPF 网格,这是我可以使用 ADO.NET/MySQL 完成的操作,没有任何问题。以下代码显示了我遇到的问题:
private void displayManagersConsole()
{
//This Works, prints out to console
conn.Open();
LinqExample linq = new LinqExample(conn);
var query = (from m in linq.Managers select m);
foreach (var manager in query)
{
Console.WriteLine(manager.FirstName);
}
conn.Close();
}
private void displayManagersWPF()
{
//This fails
conn.Open();
LinqExample linq = new LinqExample(conn);
peopleGrid.ItemsSource = linq.Managers;
conn.Close();
}
如您所见,我有两种方法。第一个方法打开一个连接,从对象获取信息,然后关闭连接。这可以正常工作,并且连接可以正常关闭。然而,当我尝试第二种方法时,它会抛出以下异常:
MATISSE-E-NOTRANORVERSION, Attempted to access objects without a transaction or version access
我已广泛阅读文档,但无法解释这一点。奇怪的是,如果我删除 conn.Close();将 itemssource 分配给我的数据网格后,它工作正常!似乎即使在分配了 itemssource 之后,它也会以某种方式对其进行缓存,以便数据网格需要打开连接。我仅在使用 wpf 组件时才会出现此行为,但所有控制台查询都可以正常工作。我还尝试在 foreach 循环中迭代并将每个 Manager 对象添加到数据网格的 Items 集合中,只是为了得到相同的异常。
我曾尝试以一种方式强制绑定一次。曾尝试扰乱交易。我什至放置了一个连接打开和连接关闭按钮,并尝试跟踪发生的情况,但如果分配了 itemssource,我根本无法关闭连接。
如果有人可以提供帮助,我将非常感激。 谢谢, 迈克
编辑:这似乎有效 工作 我使用的解决方案是只获取我想要的列。因此,它似乎与延迟加载/渴望加载有关。然而,该文档似乎没有评论如何修改它。感谢您的输入,我将使用这个:
private void displayManagersWPF()
{
conn.Open();
conn.StartTransaction();
Example db = new Example(conn);
peopleGrid.ItemsSource = (from m in db.Managers
select new
{
m.FirstName,
m.LastName,
m.Title
}
);
conn.Close();
}
I am using the Matisse DB with .NET bindings to fetch objects from the database and populate a WPF datagrid. The database has generated me a LINQ context which I can use to retrieve objects from the database. I am trying to simply bind the contents of an object to a WPF grid, something I can do with ADO.NET/MySQL with no issues. The following code shows the issue I have:
private void displayManagersConsole()
{
//This Works, prints out to console
conn.Open();
LinqExample linq = new LinqExample(conn);
var query = (from m in linq.Managers select m);
foreach (var manager in query)
{
Console.WriteLine(manager.FirstName);
}
conn.Close();
}
private void displayManagersWPF()
{
//This fails
conn.Open();
LinqExample linq = new LinqExample(conn);
peopleGrid.ItemsSource = linq.Managers;
conn.Close();
}
As you can see, I have two methods. The first method opens a connection, gets info from the object and then closes the connection. This works without problem and the connection closes fine. However when I try the second method it throws the following exception:
MATISSE-E-NOTRANORVERSION, Attempted to access objects without a transaction or version access
I have read extensively through the documentation and can not explain this. The strange thing is, if i remove the conn.Close(); after assigning the itemssource to my datagrid, it works fine! It seems that even after the itemssource has been assigned, it is somehow caching it, such that the datagrid needs the connection to be open. I get this behaviour only when using wpf components, yet all console queries work without problem. I have also tried iterating in a foreach loop and adding each Manager object to the Items collection of the datagrid, only to get the same exception.
I have tried forcing the binding as one way, one time. Have tried messing with transactions. I even put in a connection open and connection close button and tried to follow whats happening, but I simply cannot close the connection if the itemssource is assigned.
If anyone could help, would really appreciate it.
Thanks,
Mike
EDIT: This seems to work
Working
The solution I used was to just get the columns I wanted. Therefore it seems likely it is relating to lazy loading/eager loading. The documentation does not however seem to comment on how this can be modified. Thanks for input, I will use this:
private void displayManagersWPF()
{
conn.Open();
conn.StartTransaction();
Example db = new Example(conn);
peopleGrid.ItemsSource = (from m in db.Managers
select new
{
m.FirstName,
m.LastName,
m.Title
}
);
conn.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
var linq = from i in dc.Managers select i.FirstName;
peopleGrid.ItemsSource = linq.ToList();
dc.Connection.Close();
var linq = from i in dc.Managers select i.FirstName;
peopleGrid.ItemsSource = linq.ToList();
dc.Connection.Close();