Ricardo Peres
Ricardo Peres

Reputation: 14555

Map Guid Property to Oracle in Entity Framework Code First

I am trying to map a Guid property to Oracle. Here's its declaration:

[Key]
[Column(Order = 0, TypeName = "RAW")]
[MaxLength(16)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid Id { get; set; }

However, I keep getting the following exception:

Schema specified is not valid. Errors:

(7,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.Guid[Nullable=False,DefaultValue=]' of member 'Id' in type 'Model.Test' is not compatible with 'OracleEFProvider.raw[Nullable=False,DefaultValue=,MaxLength=2000,FixedLength=False]' of member 'Id' in type 'CodeFirstDatabaseSchema.Test'.

What am I missing?

Thanks!

RP

Upvotes: 4

Views: 4101

Answers (3)

Johnny Wu
Johnny Wu

Reputation: 1538

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()");
});

Upvotes: 0

George Som
George Som

Reputation: 11

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.

Upvotes: 1

Ricardo Peres
Ricardo Peres

Reputation: 14555

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.

Upvotes: 4

Related Questions