为什么 Database First EF 会跳过映射某些表?
我正在使用 Entity Framework 4 和 Database First 绑定,并且 EF 不会为我的一些表生成实体。我没有收到任何错误,并且无论我从设计界面上的“从数据库更新模型”弹出菜单中选择要生成的表多少次,模型中仍然缺少相同的表。
我在向导中没有收到任何错误。它们只是不会生成。有什么线索吗?
I'm using Entity Framework 4 and with a Database First binding, and EF is not generating the entities for a few of my tables. I'm not getting any errors, and no matter how many times I select the tables to generate from the "Update Model from Database" popup menu on the design surface, the same tables are still missing from the model.
I get no errors in the wizard. They just don't get generated. Any clues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
EF 需要表上有主键。 EF 不会映射无法找到或派生主键的表。如果所有列都可以为空,则不能假定主键。如果一列或多列不可为空,EF 显然会为表派生主键。
EF requires a primary key on the table. EF will not map tables for which it can't find or derive a primary key. If all columns are nullable, it can't assume a primary key. If one or more columns are not nullable, EF will evidently derive a primary key for the table.
EF 将忽略没有主键的表。
EF will ignore table without primary keys.
我能想到的选项:
您是否选中了这些表格旁边的框?
您之前是否添加了它们,然后删除了它们的实体,但保留了表的缓存?
如果是这样,您可以从实体浏览器窗口中删除它们并重新添加它们
或者手动添加实体并在映射窗口中定义它们映射到的表。
也许表被分类为关系而不是实体?
Options I can think of:
Did you check the box next to those tables?
Did you previously add them, then delete their entities but keep the cache of the tables?
If so you can remove them from entity browser window and re-add them
or manually add entities and define the table they map to in mappings window.
Perhaps tables were classified as relations instead of entities?
实际上,就我而言,它不起作用,因为我使用 Hierarchyid 字段作为主键,而 EF 不适用于此字段类型,因此,它没有导入表,因为需要有效的 PK。
Actually, in my case, it doesn't work because I was using a hierarchyid field as a primary key and EF doesn't work with this field type, so, it didn't import the table, because a valid PK is required.
一种可能性是当您使用具有某些不同字段类型的表时,如 SQL Server 中的层次结构。
A possibility is when you're using tables with some different field types, as hierarchy in SQL Server.
如果没有主键表,在 EF 上会自动跳过,否则您会将值修复为非空。
Without Primary Key Tables where Skip Automatically on EF, OtherWise You Fix a Value as Not Null.