问题
We are developing WPF application using entity framework (DB First) in VS2012. We are experiencing a problem in datagrid view. We dragged a datagrid from data sources which created a datagrid bound to that particular table. The table has two columns, one is transporterID which is PK and is autogenerated. The other is transportername. The problem is when we run the application, it displays data in data grid but primary key field is always zero. It should be auto increment.
The google search about this returned that i should set StoreGeneratedPattern to Identity, which i did. Also i edited my edmx file in xml editor and checked that StoreGeneratedPattern is set to Identity.
Nothing changed that zero to auto increment.
回答1:
private void Button_Click(object sender, RoutedEventArgs e)
{
using(rDatabaseEntities rde = new rDatabaseEntities())
{
rde.TransporterId = dataGridView.rows.count-1;
rde.SaveChanges();
}
}
You can manually try to set transportedId.
回答2:
What i was doing is, i was using my own context even if VS auto generated for me. What i did is i removed that autogenerated context fro window_loaded event and declared it outside.
See here
来源:https://stackoverflow.com/questions/29274469/primary-key-in-datagrid-is-always-zero