WPF DataGrid Unable To Close DB (Matisse) Connection

290 views Asked by At

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();
    }
1

There are 1 answers

2
demo.b On BEST ANSWER

var linq = from i in dc.Managers select i.FirstName;

peopleGrid.ItemsSource = linq.ToList();

dc.Connection.Close();