简体   繁体   中英

DbContext is empty using EF6

I used Visual Studio 2013 to generate the model from an existing database. The model is generated and I was able to add few entries to the database using this model. However, whenever I try to read from the DB all entries are returned as null.

I'm using this code:

using (MaintenanceDB db = new MaintenanceDB())
{
    try
    {
        var data = from e in db.employees select e;
        dataGrid.ItemsSource = data;                        
    }
    catch (Exception)
    {
    }
}

the variable data is empty and nothing is appearing in the datagrid. I tried to use the debugger to check this in the Count property is 0

I tried using:

db.employees.Find("H1992");

In this case I can find the wanted entry. But it only appears in the datagrid if I used db.employees.Local

data is only the query. If you want to access items you will have to materialize the query with ToList() or ToArray() for instance.

Anyway I don't get the point why you use a linq statement either - there is no need for it!

You could direclty assign dataGrid.ItemsSource = db.employees; Please see MSDN for a detailed explanation on LINQ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM