我用的是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相关问答推荐

Select 同一张表中的结果数?

MySQL函数 - 如何对select查询应用多个条件

MySQL:根据条件查找某些用户的行位置

Mysql Pivot 查询/建议

为什么从我的 SQL 查询中删除 BINARY 函数调用会如此显着地改变查询计划?

按长度过滤 varbinary 字段

如何在 Windows 上访问 xampp 的命令行

MySQL:如何多次加入同一张表?

MySql 以秒为单位的两个时间戳之间的差异?

安装 mysql-python (Windows)

PHP 判断 NULL

NodeJS/mySQL - ER - ACCESS - DENIED - ERROR 用户'root'@'localhost'的访问被拒绝(使用密码:是)

在 PHP/MySQL 中将日期时间存储为 UTC

将 JavaScript 到日期对象转换为 MySQL 日期格式 (YYYY-MM-DD)

如何将 mysqldump 的输出拆分为较小的文件?

如何将 MySQL 查询结果存储在另一个表中?

BIGINT mysql 性能与 INT 相比

在 MySQL 中解析日期

从 PDO 准备好的语句中获取查询

等价于在 MySQL 中处理字符串的 explode()