繁体   English   中英

Mysql 实体框架上的 Guid 属性

[英]Guid property on Mysql Entity Framework

我在 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. 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)" });
}

我有这个问题,我解决了这个问题:

  1. 我从我的项目中删除了MySql.Data.EntityFrameworkCore

  2. 我安装了NugetNuget ,这个数据提供程序正确地映射了Guid

  3. 我将我的方法名称UseMySQL更改为UseMySqlusing Microsoft.EntityFrameworkCore;添加using Microsoft.EntityFrameworkCore; Startup.cs

services.AddDbContext(options =>
options.UseMySql(Configuration.GetConnectionString( “MyContext”)));

请记住确保Guid是数据库中的数据类型char(36)

我已经使用Database First方法在常规ASP.NET(而不是Core)中成功使用了带有MySql EF的Guid类型。

为此,我将MySql中的列类型定义为BINARY(16) 自从我开发了这个模型以来已经有一段时间了,但我想我记得MySql EF提供程序会自动将BINARY(16)映射到EF模型中的System.Guid

您将不得不使用oldguids=true连接字符串属性,以便MySql使用BINARY(16)类型作为guids而不是默认的CHAR(36)

我还认为在连接字符串中创建列CHAR(36)并跳过oldguids=true部分也会导致相同的事情。

在 Mihai 的回答之上,我遵循了相同的方法,但只是想问一下,如果我们使用十六进制函数将二进制 16 转换为 UUID,我们是否期望相同的 Guid 值?

因为现在我得到了不同的值,将在 C# 中生成的 Guid 与从 MySQL Binary 16 列转换的 UUID 进行比较。

不知道其他人是否有同样的问题?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM