With Database First approach i've created the entities (with entity framework) inside my wpf project so i have the edmx file.
From MSDN: EF generates code from your model using T4 templates. The templates shipped with Visual Studio or downloaded from the Visual Studio gallery are intended for general purpose use. This means that the entities generated from these templates have simple ICollection properties. However, when doing data binding using WPF it is desirable to use ObservableCollection for collection properties so that WPF can keep track of changes made to the collections. To this end we will to modify the templates to use ObservableCollection.
So i've followed this tutorial to change the entities to have ObservableCollection properties: http://msdn.microsoft.com/en-us/data/jj574514.aspx (Section Updating code generation for data binding)
In the WPF view (in the xaml file) with Visual Studio i've added a DataGrid and added this code:
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
using (SIEntities siContext = new SIEntities())
{
var query = from p in siContext.Customers
select p;
dataGrid.ItemsSource = query.ToList();
}
}
In a first istance, to learn how insert data, i want from code insert a new customer in the database so i've this method:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
using (SIEntities siContext = new SIEntities())
{
Customer cust1 = new Customers();
cust1.Name = "Pippo";
cust1.City = "London";
siContext.Customers.Add(cust1);
siContext.SaveChanges();
dataGrid.Items.Refresh();
}
}
With this code i'm able to insert a new row in the database but i don't see this new row in the datagrid.
in the xaml file i've the following for datagird:
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="43,65,0,0" VerticalAlignment="Top" Height="234" Width="423"/>
Why? The dataGrid it's not binding to the entity? How can i show the new row added in the db also in the datagrid?
Thanks