DataBinding与entityframework错误

时间:2010-07-04 14:41:53

标签: c# .net entity-framework data-binding ado.net

我有一个应用程序,它包含简单的DB模式:

网络1 - > *商店

我使用带有winforms的entityframework(Default EntityObject Code Generator), 我使用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插入列'Location',表'MyDB.dbo.Shops';列不允许空值

我的问题是为什么它也不会出现在第一个场景中(我也不允许在网络表中使用NULL)?

感谢。

1 个答案:

答案 0 :(得分:-1)

无法将值NULL插入列'Location',表'MyDB.dbo.Shops';列不允许空值表示您的表不接受NULL。

因此,如果列可以为空,您可以在表中查看。如果它不可为空,那么当你说它在第一个场景中成功时,请看看你的数据库。不知何故,你必须在表格中输入一些默认值。

相关问题