Map Guid Property to Oracle in Entity Framework Code First Map Guid Property to Oracle in Entity Framework Code First oracle oracle

Map Guid Property to Oracle in Entity Framework Code First


I have given up, since I got no response from either Oracle or Microsoft. As far as I can tell, there is no way to have Entity Framework Code First use Guids on Oracle.I am using a String instead of the Guid, but I still populate it with a Guid.NewGuid().ToString(), so that I have a unique primary key.


The issue here is that there is no direct mapping of the Oracle Data Type of "RAW(16)" to the EDM type of "Guid". You can find this information in the Developer Guide at:

https://docs.oracle.com/database/121/ODPNT/entityEDMmapping.htm#ODPNT8275

We had run into the same exact issue, but was able to solve it by simply removing the configuration of the "TypeName" property on the [Column] attribute. Below is a sample of how we decorated our code-first Entity property.

[Column("ColumnName")]public Guid Uid{ get; set; }

I don't know why this work, but it just does. Hopefully Oracle updates their EF library to be able to use something like TypeName = "RAW(16)". Hope this helps.


I did a reverse engineering to see how Oracle handles the RAW primary key and here's what I have found:

Existing Database Setup:

Column Name: MSID

Type: RAW

Length: 20

Default: SYS_GUID()

Generated Model (scaffold-dbcontext):

[Column("MSID")][MaxLength(20)]public byte[] Msid { get; set; }

Generated Context:

modelBuilder.Entity<MsmSample>(entity =>{  entity.HasKey(e => e.Msid)        .HasName("MSM_SAMPLE_PK");  entity.HasIndex(e => e.Msid)        .HasName("MSM_SAMPLE_PK")        .IsUnique();  entity.Property(e => e.Msid).HasDefaultValueSql("sys_guid()");});