VS错误:列' IsPrimaryKey'的值在表格' TableDetails'是DBNull

时间:2016-08-22 03:32:08

标签: c# mysql entity-framework visual-studio phpmyadmin

我正在使用带有Entity Framework 6和MySQL Server 5.7.13的Visual Studio 2015 mysql服务器来自在线的web和mysql服务器托管服务,我使用phpmyadmin访问它。

当我尝试从数据库更新/生成模型(服务器连接成功)时出现以下错误

Unable to generate the model because of the following exception: 'System.Data.StrongTypingException: The value for column 'IsPrimaryKey' in table 'TableDetails' is DBNull. ---> System.InvalidCastException: Specified cast is not valid.
   at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.SchemaDiscovery.TableDetailsRow.get_IsPrimaryKey()
   --- End of inner exception stack trace ---
   at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.SchemaDiscovery.TableDetailsRow.get_IsPrimaryKey()
   at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.StoreModelBuilder.CreateProperties(IList`1 columns, IList`1 errors, List`1& keyColumns, List`1& excludedColumns, List`1& invalidKeyTypeColumns)
   at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.StoreModelBuilder.CreateEntityType(IList`1 columns, Boolean& needsDefiningQuery)
   at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.StoreModelBuilder.CreateEntitySets(IEnumerable`1 tableDetailsRows, EntityRegister entityRegister, IList`1 entitySetsForReadOnlyEntityTypes, DbObjectType objectType)
   at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.StoreModelBuilder.CreateEntitySets(IEnumerable`1 tableDetailsRowsForTables, IEnumerable`1 tableDetailsRowsForViews, EntityRegister entityRegister)
   at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.StoreModelBuilder.Build(StoreSchemaDetails storeSchemaDetails)
   at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelGenerator.CreateStoreModel()
   at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelGenerator.GenerateModel(List`1 errors)
   at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelBuilderEngine.GenerateModels(String storeModelNamespace, ModelBuilderSettings settings, List`1 errors)
   at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelBuilderEngine.GenerateModel(ModelBuilderSettings settings, IVsUtils vsUtils, ModelBuilderEngineHostContext hostContext)'.
Loading metadata from the database took 00:00:00.6311313.
Generating the model took 00:00:01.9805600.

这个问题已被提出并多次回答,大多数人都同意这些解决方案中的任何一个都能解决问题:
- 降级到MySQL 5.6 - 设置全局optimizer_switch =' derived_merge = OFF' (需要超级特权
- 设置@@ optimizer_switch =' derived_merge = OFF'

我采访了支持(来自我的phpmyadmin工具)并请他们帮我解决上述任一解决方案,这是他们的回复:

The MySQL version on all servers was very recently upgraded to MySQL 5.7.

Indeed, super user privileges are not available for your MySQL users. 
Also, it is not technically possible to revert the MySQL version back to 5.6. 

我进一步问他们为什么这些解决方案是不可能的,因为这些是我问题的唯一已知解决方案,这是他们的最终答复:

Please note that your account is hosted on a shared hosting server, 
and there are lots of other users whose accounts are located on the same server 
where your account is hosted, hence we cannot globally turn off that feature, 
as this would affect negatively the performance of the database server for 
all the hosting accounts on the server (including your account as well).

所以我向社区提出的最后一个问题:此处的任何人都知道除上面列出的解决方案之外的任何解决方案(特定于我的案例和限制)

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题并在此处使用了答案解决了:

https://bugs.mysql.com/bug.php?id=79163#c445530

在MySQL中

use <<database name>>;
set global optimizer_switch='derived_merge=off';
set optimizer_switch='derived_merge=off';

答案 1 :(得分:0)

由于我无法及时找到有效的解决方案,而且我的问题很紧急,我不得不找到解决方法。

我使用了不同的mysql服务器托管平台并在那里迁移了我的数据库。它使用的是MariaDB,因此mysql版本较低(5.6)。

最后,我使用Visual Studio数据连接来正常连接到新数据库,并且它工作正常。