9

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)" });
}
erexo
  • 503
  • 1
  • 6
  • 24
  • You should use `char(32)` or wider field in DB and in EF `string`, for storing GUID. You can update the table using EF update database wizard or can make manual changes to the EF db XML file. Change guid to string and in table structure to char(36). – Allen King Jul 15 '17 at 16:19
  • Is there any way to define Guid as `char(32)` or `binary(16)` for MySql? So I could use Guid in my application without any wrappers. – erexo Jul 16 '17 at 22:41

2 Answers2

17

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.

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
Anderson Paiva
  • 761
  • 9
  • 14
5

I have successfully used the Guid type with MySql EF in regular ASP.NET (not Core) with the Database First approach.

To accomplish this, I've defined the column type in MySql as BINARY(16). It's been quite a while since I've developed that model, but I think I remember that MySql EF provider automatically maps BINARY(16) to System.Guid in the EF models.

You will have to use the oldguids=true connection string property so that MySql uses the BINARY(16) type for guids instead of the default CHAR(36).

I also think that making the column CHAR(36) and skipping the oldguids=true part in the connection string would also result in the same thing.

Mihai Caracostea
  • 8,336
  • 4
  • 27
  • 46
  • I have added `oldguids=true` to the connection string but nothing changed. I want to solve this problem but using code first. Thanks for your response – erexo Jul 16 '17 at 22:39