Pergunta

I have a VS11 beta WPF application with a Microsoft Datagrid based on a table from an Entity Framework 5.0b2 model (from an sql server database). (The grid uses whatever code was autogenerated by adding a data source from an entity framework model and dragging and dropping a table from that model to the design surface.). I then used the EF5.0 DBContext code generator add-in to generate DbContext types that I could use in the app.

The result was not good. I got a compile error that seems unfixable; at least my attempts made things worse. The datagrid does not like DbContext and doesn't co-exist with it (when both the datagrid and DbContext are based on the same database and tables). Is there a workaround, maybe by changing the code generator template?

The error message is pasted at the end. Thanks for any help or insight on this.

(note the following closely related post isn't an answer because though I can convert dbcontext to objectcontext, I can't get datagrid to work properly with it when I do; and if I convert objectcontext to dbcontext, I don't have the autogenerated dbcontext types: Convert DBContext to ObjectContext for use with GridView). I'm looking for a fairly simple workaround--I'm sure there's a workaround if I start hand coding all types and conversions, but I want to keep the ease of using the autogenerated code--I don't want to be occupied with work the code generator can, and so should, do. If there isn't a solution, I suppose I'll base the controls on ado.net and leave entity framework for non-ui code.

Error
Cannot implicitly convert type 'System.Data.Entity.DbSet' to 'System.Data.Objects.ObjectQuery'

Documents\Visual Studio 11\Projects\WpfApplication3\WpfApplication3\MainWindow.xaml.cs

The line that caused the error (created by Microsoft drag and drop of the datagrid with an EF table as the source): System.Data.Objects.ObjectQuery myTblsQuery = myDbsEntities.MyTbls;

Foi útil?

Solução

The code gnerated by drag and drop in the WPF designer was never very good and the WPF team haven't updated it to work with DbContext. Your best bet is either to drop down to ObjectContext like the other answers suggest or to use the much better WPF data binding supported by DbContext but do so without using drag-and-drop.

WPF data binding with DbContext is pretty easy because the Local property of each DbSet is actually an ObservableCollection which WPF naturally binds to very well. So for example you end up binding your view sources something like this:

categoryViewSource.Source = _context.Categories.Local;

This blog post contains more details: http://blogs.msdn.com/b/adonet/archive/2011/03/14/10138486.aspx

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top