【发布时间】:2017-12-20 13:39:43
【问题描述】:
我在 ASP.NET Core 应用程序上使用带有 MySql 扩展的实体框架。我的一个域模型Message 具有 Guid 属性,当我想对我的 DbContext 执行任何操作时,我收到一个错误:The property 'Message.ID' is of type 'Guid' which is not supported by current database provider. Either change the property CLR type or manually configure the database type for it.。
如何“手动配置数据库类型”?我读过它应该映射为 CHAR(36),但我在应用程序端找不到如何做到这一点。
@更新
当我将属性[Column(TypeName = "char(32)")] 设置为Guid 属性时,错误仍然存在。
这个方法也不行
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var messageEntity = modelBuilder.Entity< Message>();
messageEntity.Property(x => x.ID).
HasAnnotation("Column", new { TypeName = "char(32)" });
}
【问题讨论】:
-
您应该在 DB 和 EF 中使用
char(32)或更宽的字段string来存储 GUID。您可以使用 EF 更新数据库向导更新表,也可以手动更改 EF db XML 文件。将 guid 更改为字符串,将表结构更改为 char(36)。 -
MySql 有没有办法将 Guid 定义为
char(32)或binary(16)?所以我可以在我的应用程序中使用 Guid 而无需任何包装器。
标签: c# mysql entity-framework asp.net-core