我用的是Visual Studio 2013Entity Framework 5和MySQL Server 5.7.9.

try 从数据库(or 'Update Model From Database')创建模型时,会出现以下消息:

'System.Data.StrongTypingException: The value for column 'IsPrimaryKey' in table 'TableDetails' is DBNull . ---> System.InvalidCastException: Specified cast is not valid.

我知道以前有人问过这个问题,但我还没有找到任何解决办法.

即使对于一个简单的表,问题也会出现.

样本表

CREATE TABLE new_table
(
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(45) NOT NULL,
  PRIMARY KEY (id)
) 
ENGINE = InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

如果表仅由主键组成,那么模型将按其应有的方式创建.

EDIT:

有人知道这件事吗?

亲切的问候.

完整的错误堆栈:

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(IList1 columns, IList1 errors, List1& keyColumns, List1& excludedColumns, List1& invalidKeyTypeColumns) at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.StoreModelBuilder.CreateEntityType(IList1 columns, Boolean& needsDefiningQuery) at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.StoreModelBuilder.CreateEntitySets(IEnumerable1 tableDetailsRows, EntityRegister entityRegister, IList1 entitySetsForReadOnlyEntityTypes, DbObjectType objectType) at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.StoreModelBuilder.CreateEntitySets(IEnumerable1 tableDetailsRowsForTables, IEnumerable1 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(List1 errors) at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelBuilderEngine.GenerateModels(String storeModelNamespace, List1 errors) at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelBuilderEngine.GenerateModel(EdmxHelper edmxHelper)'. Loading metadata from the database took 00:00:00.5856317.

推荐答案

Entity Framework (version 6.1.3) and MySQL Server (>= 5.7.6)

解决这个问题的一个方法是,

1. Open Services (services.msc) and restart MySQL57 service.
2. Execute the following commands in MySQL.
   use <<database name>>;
   set global optimizer_switch='derived_merge=OFF';
3. Update the .edmx.

回复晚了.但希望它能帮助别人.

谢谢

Mysql相关问答推荐

MySQL如何通过外键ID选择多行?

了解 SQL 中的元组语法

在 MySQL 中转换 JSON 数组值

如何查询打印已售罄的产品? [MYSQL]

具有别名主机的 ssh 反向 mysql tunel

FreeBASIC 中的 MySQL 访问读取

结果差异(MySQL 5.7 vs MySQL 8.0)

绕过 MySQL“无法重新打开表”错误

如何将 MySQL 5.7 更新到新的 MySQL 8.0?

Spring Boot:Jdbc javax.net.ssl.SSLException:在接收对等方的 close_notify 之前关闭入站

pip install mysqlclient 返回“致命错误 C1083:无法打开文件:'mysql.h':没有这样的文件或目录

仅获取今天在 laravel 中创建的记录

无法加载身份验证插件“caching_sha2_password”

表被指定了两次,既作为“更新”的目标,又作为 mysql 中数据的单独源

ER_NOT_SUPPORTED_AUTH_MODE - MySQL 服务器

Yii2 如何进行 where AND 或 OR 条件分组?

PDO::__construct():服务器向客户端发送未知字符集 (255).请向开发人员报告

MySQL 5.7.12 导入无法从具有 CHARACTER SET 'binary' 的字符串创建 JSON 值

MySQL 错误: '访问被拒绝用户'root'@'localhost'

utf8mb4_unicode_ci 与 utf8mb4_bin