与实体框架的数据绑定错误
我有一个应用程序,其中包含简单的数据库模式:
网络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;
所有数据绑定都工作良好且同步,我可以在表单上的两个网格上进行 CUD 并毫无问题地转到 _context.SaveChanges()
。
- 第一个场景
一个简单的场景,在NetworkBindingNavigator
上按“+”(添加),然后在网格上的空行上按“X”(删除),最后我走了到 context.SaveChanges() 毫无问题地成功。
- 第二个场景
当我在ShopBindingNavigator
上按“+”(添加),然后我在网格上的这个空行上按“X”(删除),最后我转到 _context.SaveChanges()
我得到:
System.Data.UpdateException:更新条目时发生错误。有关详细信息,请参阅内部异常。 ---> System.Data.SqlClient.SqlException:无法将 NULL 值插入表“MyDB.dbo.Shops”的“Location”列;列不允许为空
我的问题是为什么在第一个场景中也没有发生这种情况(我也不允许网络表中出现空值)?
谢谢。
I have an application which consists of simple schema of DB:
Networks 1-->* Shops
i use entityframework (Default EntityObject Code Generator) with winforms,
i use DataBinding to a grid to CUD these entities,
i have :
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;
all databinding is working good and synchronized, i can CUD on both grids on the Form and go to _context.SaveChanges()
with no problem.
- First Scenario
a simple scenario of Pressing "+"(add) on the NetworkBindingNavigator
and right afterwards "X"(delete) on this empty line on the grid and finally i go to context.SaveChanges()
succeed without a problem.
- Second Scenario
when i press "+"(add) on the ShopBindingNavigator
and then right afterwards i press "X"(delete) on this empty line on the grid and finally i go to _context.SaveChanges()
i get :
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
my question is why didnt it happen in the first scenario as well (i dont allow NULL in Networks table as well) ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法将 NULL 值插入表“MyDB.dbo.Shops”的“Location”列;列不允许空值意味着您的表不接受空值。
那么您可以检查表中的列是否可为空。如果它不可为空,那么当您说它在第一个场景中成功时,请查看您的数据库。您必须以某种方式在表中输入一些默认值。
Cannot insert the value NULL into column 'Location', table 'MyDB.dbo.Shops'; column does not allow nulls means you table does not accept NULL.
so can you check in the table if the column is nullable or not. If it is not nullable then when you say it has succeeded in your first scenario have a look at you DB. Somehow you must be inputting some default values into the tables.