问题描述
我正在ASP.NET Core应用程序上使用带有MySql扩展的Entity Framework。我的域模型之一 Message
具有Guid属性,当我想在DbContext上执行任何操作时,我收到一个错误消息:属性'Message .ID的类型为 Guid,当前数据库提供程序不支持。
。
I'm using Entity Framework with MySql extension on ASP.NET Core application. One of my domain model Message
have Guid property and when I want to execute any operation on my DbContext I'm receiving an error: 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),但我找不到在应用程序端执行该操作的方法。
How do I "manually configure the database type'? I've read that it should be mapped for CHAR(36), but I couldn't find how to do that on application side.
@UPDATE
当我将属性 [Column(TypeName = char(32))]
设置为 Guid
属性,错误仍然存在。
When I set the attribute [Column(TypeName = "char(32)")]
to the Guid
property, error remains.
此方法也不起作用
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var messageEntity = modelBuilder.Entity< Message>();
messageEntity.Property(x => x.ID).
HasAnnotation("Column", new { TypeName = "char(32)" });
}
推荐答案
我遇到了这个问题,并以这种方式解决了:
I had this issue and I resolved that way:
-
我从项目中删除了MySql.Data.EntityFrameworkCore
I removed the MySql.Data.EntityFrameworkCore from my project
我安装了由 Nuget
和
数据提供者已正确映射 Guid
。
我将方法名称 UseMySQL
更改为 UseMySql
并在 Startup.cs
处使用Microsoft.EntityFrameworkCore; 添加了:
I changed my method name
UseMySQL
to UseMySql
and added using Microsoft.EntityFrameworkCore;
at Startup.cs
:
services.AddDbContext(options =>
options.UseMySql(Configuration.GetConnectionString( MyContext))))
services.AddDbContext(options =>
options.UseMySql(Configuration.GetConnectionString("MyContext")));
记住确保
Guid
的数据类型为 char(36)
在数据库中。
Remember to make sure the
Guid
is data type char(36)
in the database.
这篇关于Mysql实体框架上的Guid属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!