Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oracle RAW column to Guid C#: Invalid Cast Exception #637

Closed
Rojiferio opened this issue Nov 3, 2016 · 15 comments
Closed

Oracle RAW column to Guid C#: Invalid Cast Exception #637

Rojiferio opened this issue Nov 3, 2016 · 15 comments

Comments

@Rojiferio
Copy link

Hi,

I'm trying to get Guid columns from Oracle Database (RAW: Generated by Entity Framework code first), and is thrown an exception. Is there some method to achieve it?

Thank you!

@mgravell
Copy link
Member

mgravell commented Nov 3, 2016

What is the exact error message? I'm hoping it gives a hint as to the data
type coming back. If it is a byte[], and specifically a byte[16], then
yes, I think can can and should support that. If it is something more
exotic... less obvious.

On 3 November 2016 at 09:10, Rojiferio notifications@github.com wrote:

Hi,

I'm trying to get Guid columns from Oracle Database (RAW: Generated by
Entity Framework code first), and is thrown an exception. Is there some
method to achieve it?

Thank you!


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#637, or mute the
thread
https://github.com/notifications/unsubscribe-auth/AABDsNtrp0j7xi3R3-jKCml7rk8zdbOQks5q6aUBgaJpZM4KoIEs
.

Regards,

Marc

@Rojiferio
Copy link
Author

Thanks for response,

the error doesn't give me too much information:

Message: "Specified cast is not valid"
Target site: "Deserialize3c1a1cf8-4ac3-44b7-add5-d45d0c40cbfc(System.Data.IDataReader)"

The column is defined as RAW in Oracle, and I want to load in a Guid property. If I get the info using EF works....

Thanks again!

@mgravell
Copy link
Member

mgravell commented Nov 3, 2016

Can you show me the code that you're using there? Because in many cases, the library injects a lot more context. I'm curious as to why that's being bypassed...

@Rojiferio
Copy link
Author

Rojiferio commented Nov 3, 2016

   [TestMethod]
    public void DapperOracle()
    {
        try
        {
            var factProvDb = ProviderDatabaseFactory.GetProviderDatabase("Oracle.ManagedDataAccess.Client", "");
            using (var ctx = DPMContextFactory.CreateDPMContext(factProvDb))
            {
                var sql = @"SELECT * from ""Units""";
                var aux = ctx.Database.Connection.Query<PersistenceModel.Entities.Unit>(sql);
            }
        }
        catch (Exception ex)
        {
            var aux = ex.Message;
        }

    }

And the query should returs that info: (Data columns defined as RAW (16))

units

@mgravell
Copy link
Member

mgravell commented Nov 3, 2016

K, I'll see what I can do. There's a slight complication, because .NET Guid does something incredibly stupid I like to call "crazy-endianness", where the byte-order from the byte[]-based methods is essentially random. For giggles. So I need to be very careful to ensure that what we actually get is what you expected.

Here's an illustration:

        var guid = Guid.Parse("00112233-4455-6677-8899-aabbccddeeff");
        var bytes = guid.ToByteArray();

        // shows "33-22-11-00-55-44-77-66-88-99-AA-BB-CC-DD-EE-FF"
        Console.WriteLine(BitConverter.ToString(bytes));
        var guid2 = new Guid(bytes);

        // shows "00112233-4455-6677-8899-aabbccddeeff"
        Console.WriteLine(guid2);

@mormegil-cz
Copy link

That is not .NET-specific, is it? That is just native Microsoft GUID format, differing in the endianness of the first three pieces from the RFC 4122 format, AFAICT.

@Rojiferio
Copy link
Author

Why method GetGuid is not suported in OracleDataReader Class?????

http://docs.oracle.com/cd/E51173_01/win.122/e17732/OracleDataReaderClass.htm#ODPNT1185

¿is this the problem?

@Rojiferio
Copy link
Author

Error parsing column 0 (Id=System.Byte[] - Object)

en Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value)
en Deserializecf1bf2a6-f889-4904-8c6e-d2c993e5e67d(IDataReader )
en Dapper.SqlMapper.d__1251.MoveNext() en System.Collections.Generic.List1..ctor(IEnumerable1 collection) en System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
en Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 commandTimeout, Nullable1 commandType)
en PersistenceUnitTes

I'm so deseperated...

@mgravell
Copy link
Member

mgravell commented Nov 3, 2016

I hope to look at this tomorrow.

@mombrea
Copy link

mombrea commented Nov 4, 2016

I saw your post and was interested. It looks like the answer is that the first 3 sets of the guid are integer types which are independently subject to big endian formatting. The remaining sets of the guid are a byte array and are not subject to endian formatting.

https://msdn.microsoft.com/en-us/library/3573497s.aspx

@mgravell
Copy link
Member

mgravell commented Nov 4, 2016

I'm have built and uploaded 1.50.3-beta1 that tries to address this. Is there any chance you could try it and let me know if it works?

mgravell added a commit that referenced this issue Nov 4, 2016
@Rojiferio
Copy link
Author

Thanks!

I expect to can ckeck your release this week , I solved my issue implmenting a custom TypeHandler for Guid:

class GuidTypeHandler : SqlMapper.TypeHandler<Guid> { public override Guid Parse(object value) { var inVal = (byte[])value; return new Guid(inVal); }

With this code I can read data without problem.

I'll answer you as soon as possible.

Thank you again!

@NickCraver
Copy link
Member

@Rojiferio just looking to close this out if we're all good here - can you please let us know when you get a chance? Thanks!

@mgravell
Copy link
Member

mgravell commented Dec 5, 2016 via email

mgravell added a commit that referenced this issue Jan 6, 2017
@mgravell
Copy link
Member

mgravell commented Jan 6, 2017

Reverted; sorry, closing this down since we can't support it natively in the lib due to the incompatibility between backends

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants