我有一个由简单的数据库模式组成的应用程序:
网络 1-->* 商店
我将 entityframework(默认 EntityObject 代码生成器)与 winforms 一起使用, 我使用 DataBinding 到网格来 CUD 这些实体, 我有:
DbObjectModelContainer _context = new DbObjectModelContainer();
_context.ContextOptions.LazyLoadingEnabled = true;
NetworkBindingSource.DataSource = _context.Networks;
ShopsBindingSource.DataSource = NetworkBindingSource;
ShopsBindingSource.DataMember = "Shops";
NetworkBindingNavigator.BindingSource = NetworkBindingSource;
ShopBindingNavigator.BindingSource = ShopsBindingSource;
NetworkDataGridView.DataSource = NetworkBindingSource;
ShopDataGridView.DataSource = ShopsBindingSource;
所有数据绑定(bind)都运行良好且同步,我可以在 Form 的两个网格上 CUD 并转到 _context.SaveChanges()
没有问题。
- 第一个场景
一个简单的场景,在 NetworkBindingNavigator
上按“+”(添加),然后在网格上的这个空行上按“X”(删除),最后我转到 context。保存更改()
毫无问题地成功。
- 第二种情况
当我在 ShopBindingNavigator
上按“+”(添加),然后在网格上的这个空行上按“X”(删除),最后我转到 _context .SaveChanges()
我得到:
System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Location', table 'MyDB.dbo.Shops'; column does not allow nulls
我的问题是为什么第一种情况也没有发生(我也不允许网络表中有 NULL)?
谢谢。
最佳答案
无法将 NULL 值插入表“MyDB.dbo.Shops”的列“Location”; column does not allow nulls 意味着你的表不接受 NULL。
如果该列是否可为空,您是否可以检查表。如果它不可为空,那么当你说它在你的第一个场景中成功时,看看你的数据库。您必须以某种方式将一些默认值输入到表中。
关于c# - DataBinding 与 entityframework 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3174999/