c# - DataBinding 与 entityframework 错误

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

我有一个由简单的数据库模式组成的应用程序:

网络 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/

相关文章:

c# - 使用NUnit从指定目录删除多个文件的单元测试方法

.net - 使用 CLR UDT 作为 SQL Server 存储过程参数

c# - Entity Framework 在循环中执行存储过程

c# - LINQ to Entities,连接两个表,然后分组并从两个表中获取列的总和

c# - 在 C# 中替换/更改空白或空字符串值

c# - 如何使用 RavenDb 默认 ID 生成器获取连续 ID

c# - ASP.NET core 2.2 web api 记录与数据保护 key 相关的警告 : how should we handle this issue?

.net - .NET Streams 是否节省内存?

c# - 为什么 Dictionary[index] 会抛出 KeyNotFoundException 但 Hashtable[index] 不会?

c# - 包管理器控制台如何知道要使用哪个连接字符串?