In WinForms 4.5, I add a EntityModel from a database.
Then, in the DataSource window, I can see the Entity Sets. However, it does not allow me to drag and drop them onto a Form.
If I create a Proxy class which just has
List<Customer> {get;set;}
Then I can view this in the DataSource Window and drag and drop.
Is the issue that the Designer is only looking for IList<>, not ObjectSet<>?
Am I doing something wrong or just the way it works in VS12?
Related
I can't find any working code for C# WPF window project that supports drag and drop object from one DataGrid to another DataGrid. Does anyone have one?
Something we're trying to figure out is how to specify the type of control we want to drag fields from databases, or entity framework objects, onto the WPF window, user control, page. In particular, we're thinking of using Telerik controls, and they don't seem to be assignable to the Data Source view in Visual Studio. But if we set the field to None, do we then drag it onto some control that can be data bound?
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;
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
I have MS SQL Server database with one table - Peoples. From these database generated Entity Framework Data model.
In WPF application I have ListBox with ItemSource = DataModel.Entities.Peoples, and two buttons - add and remove People in database.
Add button:
DataModel.Entities.AddPeople(new People("test"));
DataModel.Entities.SaveChanges();
And removing:
DataModel.Entities.Remove((People)listBox1.SelectedItem);
DataModel.Entities.SaveChanges();
When I click remove button - corresponding People row removing from databases and listBox1 refreshing. But when I click add button - People added in database (see in MS SQL Enterprise Manager), but listbox does not refreshing.
How to refresh listBox on adding? Guess I forgot to set any option in DataModel?
Unless DataModel.Entities.Peoples is an ObservableCollection, it won't be aware of changes.
I recommend you use the MVVM pattern for this, which solves that problem perfectly.
I am getting started with Entity Framework using EF4 in VS 2010 RC. So far, I have done some simple console apps where I create an EDM, query it using LINQ to Entities, and output the results to the console.
Now I am building a demo WPF app to learn how to integrate EF4 with WPF. I use MVVM in my WPF apps, where each view (more or less) has a view model that includes data properties to which controls are bound. For example, my WPF demo app has a Customers property in the view model to which a dropdown in the view is bound. And as you may have guessed, my EDM contains a Customer entity.
Here is my question: How do I connect the LINQ to Entities results to my view model property? When I query against my Customer entity, it appears that I get an IQueryable<Customer> back. But my view model property is of type ObservableCollection<Customer>, which I need for the data bindings to work. So, how do I get from the IQueryable<Customer> to ObservableCollection<Customer>? Thanks for your help.
According to MSDN, the ObservableCollection constructor can take a List<T> or IEnumerable<T>. Let's suppose queryable is your IQueryable<Customer>:
ObservableCollection<Customer> ocCustomer = new ObservableCollection<Customer>(queryable.ToList());