Search code examples
c#mysqlentity-frameworkasp.net-core

Guid property on Mysql Entity Framework


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..

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

When I set the attribute [Column(TypeName = "char(32)")] to the Guid property, error remains.

This method also does not work

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  var messageEntity = modelBuilder.Entity< Message>();
  messageEntity.Property(x => x.ID).
   HasAnnotation("Column", new { TypeName = "char(32)" });
}

Solution

  • I had this issue and I resolved that way:

    1. I removed the MySql.Data.EntityFrameworkCore from my project

    2. I installed Pomelo.EntityFrameworkCore.MySql by Nuget and this data provider mapped correctly the Guid.

    3. I changed my method name UseMySQL to UseMySql and added using Microsoft.EntityFrameworkCore; at Startup.cs:


    services.AddDbContext<MyDbContext>(options =>               
    options.UseMySql(Configuration.GetConnectionString("MyContext")));
    

    Remember to make sure the Guid is data type char(36) in the database.