none
Map Guid Property to Oracle in Entity Framework Code First RRS feed

  • Question

  • Hello,

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

     

    [Key]

    [Column(Order = 0, TypeName = "RAW")]

    [MaxLength(16)]

    public Guid Id getset}


    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

    Sunday, September 15, 2013 2:10 PM

Answers

  • Since I got absolutely no help from either Microsoft or Oracle, I gave up. As far as I can tell, there is no way, using Entity Framework Code First, to work with Guids on a Oracle database. I have resorted to using a String primary key, which I initialize with a Guid.

    Tuesday, September 17, 2013 2:57 AM

All replies

  • Hi,

    I have try it like this below:

     class GuidClass
        {
            [Key]
            [Column(Order = 0)]
            [MaxLength(16)]
            public int Id { get; set; }
        }

    This is ok.

    I think maybe the TypeName = "RAW" is not compatible with 'OracleEFProvider.raw'.

    Regards.

    Monday, September 16, 2013 2:07 AM
  • Is this a joke?

    I explicitly asked for Guid mapping, not int!

    RP

    Monday, September 16, 2013 7:04 AM
  • Hello Ricardo Peres,

    Welcome to this forum.

    With your description, I try it like FredBaoBao but with Guid and it works ok. The GuidClass:

    class GuidClass
    
        {
    
            [Key]
    
            [Column(Order = 0)]
    
            [MaxLength(16)]
    
            public Guid Id { get; set; }
    
    }
    

    The Result:

    CREATE TABLE [dbo].[GuidClasses] (
    
        [Id] UNIQUEIDENTIFIER NOT NULL,
    
        CONSTRAINT [PK_dbo.GuidClasses] PRIMARY KEY CLUSTERED ([Id] ASC)
    
    );
    

    I just delete the TypeName=”RAM”, because the error message says that the TypeName = "RAW" is not compatible with 'OracleEFProvider.raw'.

    Have a try.

    If this does not work for you, please let me know.

    Thanks & Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 16, 2013 9:45 AM
    Moderator
  • But you are talking about SQL Server, and I asked about Oracle!

    Can you please, PLEASE, read the question before answering?

    Thanks anyway...

    RP

    Monday, September 16, 2013 9:51 AM
  • Hi,

    Sorry for ignore that you use the Oracle.

    However I find that it may not be the DB problem. It may be a type mapping problem regarding entity framework.

    As far as I know, the RAW type is like with CHAR( I am sorry that I have sqlserver only), so I have a try with beolw:

     class GuidClass
        {
            [Key]
            [Column(Order = 0, TypeName = "Char")]
            [MaxLength(16)]
            public Guid Id { get; set; }
        }

    And the error is like with yours:

    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 'Code_First.GuidClass' is not compatible with 'SqlServer.char[Nullable=False,DefaultValue=,MaxLength=8000,Unicode=False,FixedLength=True]' of member 'Id' in type 'CodeFirstDatabaseSchema.GuidClass'.

    Because the Guid is mapped to be UNIQUEIDENTIFIER type in sqlserver, so I change it to be like below:

     class GuidClass
        {
            [Key]
            [Column(Order = 0, TypeName = "UNIQUEIDENTIFIER")]
            [MaxLength(16)]
            public Guid Id { get; set; }
        }

    And it works fine.

    So you can have a try to change the RAM to the mapped type in Oracle for Guid.

    Thanks & Regards.



    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 16, 2013 12:14 PM
    Moderator
  • Fred,

    In SQL Server this works without any problem, no need for attributes, explicit type mappings, etc. The problem is with Oracle.

    In Oracle, GUIDs are represented as RAW(16), meaning, an array of bytes. That is why I am supplying the type name. I need to know how to map a .NET Guid to the RAW(16) type.

    RP

    Monday, September 16, 2013 12:26 PM
  • Since I got absolutely no help from either Microsoft or Oracle, I gave up. As far as I can tell, there is no way, using Entity Framework Code First, to work with Guids on a Oracle database. I have resorted to using a String primary key, which I initialize with a Guid.

    Tuesday, September 17, 2013 2:57 AM
  • I am having the same issue. byte array works (byte[]) but it return bytes not the guid which is frustrating. So while setting it you have to convert your guid to byte[] and then while fetching it you convert the byte array back to guid. Still looking for a better solution.

    RAW in oracle is similar to varbinary in SQL Server and byte[] works with varbinary. That is how I got the clue that byte[] might work and it works. 


    developer


    • Edited by fifthtech Sunday, September 13, 2015 4:26 PM
    Sunday, September 13, 2015 4:25 PM