wpf 表/视图没有定义主键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14216995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The table/view does not have a primary key defined
提问by demo_user
I am trying to use the Databinding with WPF and Entity Framework.
我正在尝试将数据绑定与 WPF 和实体框架一起使用。
After adding the data-source to my project, I saved my project in order to see whether everything is fine till what I have done.
将数据源添加到我的项目后,我保存了我的项目,以查看在我完成之前是否一切正常。
but I have a 11 warn/info messages stating :
但我有 11 条警告/信息消息指出:
The table/view 'MyDataBase.TableName' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.
表/视图“MyDataBase.TableName”没有定义主键。已推断出键并将定义创建为只读表/视图。
I have 14 Tables where 4 tables has their own primary keys and others tables are referred to each other using foreign keys
我有 14 个表,其中 4 个表有自己的主键,其他表使用外键相互引用
What is this issue all about? Kindly help
这个问题到底是怎么回事?请帮忙
采纳答案by Ladislav Mrnka
Every entity in EF must be uniquely identifiable. EF uses database primary keys to define entity keys. While it is possible to work without primary keys to some extend it is definitively not recommended because it leads to many issues. The core issue is mentioned directly in the error - if the table doesn't have primary key, EF marks it as read only because it is not sure that it can uniquely identify record to be updated.
EF 中的每个实体都必须是唯一可识别的。EF 使用数据库主键来定义实体键。虽然在某种程度上可以在没有主键的情况下工作,但绝对不推荐,因为它会导致许多问题。错误中直接提到了核心问题——如果表没有主键,EF 会将其标记为只读,因为它不确定它可以唯一标识要更新的记录。
回答by John Peters
Indeed it is just a warning but you will never be able to use Save functions which in my case makes EF worthless to me for the poorly designed DB I'm stuck working on.
实际上,这只是一个警告,但您将永远无法使用 Save 函数,在我的情况下,这使 EF 对于我一直在处理的设计不佳的 DB 毫无价值。
回答by xwpdev
Define primary keys in your tables and update the Entity Data Model. You can do that after you open your EF model and right click and select update. Select refresh option from the window and validate your model.
在表中定义主键并更新实体数据模型。您可以在打开 EF 模型并右键单击并选择更新后执行此操作。从窗口中选择刷新选项并验证您的模型。


回答by Ammar
you only have to delete the EDMX file from your project. now move to your database and add primary keys to each table. After that add new EDMX to your project. it will work fine. and don't forget to remove the connectionString tag from your configuration file in project.
您只需从项目中删除 EDMX 文件。现在转到您的数据库并为每个表添加主键。之后,将新的 EDMX 添加到您的项目中。它会正常工作。并且不要忘记从项目中的配置文件中删除 connectionString 标记。

