Primary key in datagrid is always zero

吃可爱长大的小学妹 提交于 2019-12-12 21:02:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!