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

根据计数按月和年对数据进行分组()

用于搜索从各种表中获得的结果的查询

如何在WooCommerce中更新pm_Virtual.meta_Value=#39;否

查询具有JSON对象数组的列时,JSON_CONTAINS的MySQL语法错误

在MySQL中查找包含无效正则表达式的行

Mysql - Select 匹配某些条件的两条记录之间经过的最大天数

我是否需要在 N+1 列上建立索引,才能有效地在 N 列上执行 SELECT,并按其他列排序?

分别针对每个不同的列 Select 聚合

正则表达式模式相当于 mysql 中的 %word%

为什么这个 NOT NULL 到 NULL 迁移会触发大量 I/O 操作?

包含 group_contact 的视图的问题

mySQL 中的主数据库和 IF 错误?

是否可以在一个查询中将字符串附加到许多匹配的行

通过 Gorm 查询模型

使用 ON DUPLICATE KEY 将列增加一定数量 MySQL NodeJS

如何根据特定条件从mysql数据库中 Select 查询

提高mysql导入速度

从mysql中的大表中快速 Select 随机行

判断多个列的一个值

将数据库从 Postgres 迁移到 MySQL