zzzprojects / EntityFramework-Extensions

Entity Framework Bulk Operations | Improve Entity Framework performance with Bulk SaveChanges, Insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL and SQLite.
https://entityframework-extensions.net
346 stars 57 forks source link

Type of value has a mismatch with column type - Couldn't store Byte[] Expected type is Guid #366

Closed NicolasGauvreau-eaton closed 4 years ago

NicolasGauvreau-eaton commented 4 years ago

Description

We have an entity like the following:

    public class ServerCredentialsAuthMethod : UserAuthMethod
    {
        // ...
        public byte[] Salt { get; protected set; }
        // ...
    }

...with a mapping like the following...

    public class ServerCredentialsAuthMethodMap : EntityTypeConfiguration<ServerCredentialsAuthMethod>
    {
        public ServerCredentialsAuthMethodMap()
        {
            // ...
            Property(m => m.Salt)
                .IsRequired()
                .HasMaxLength(15); // NOTE: For some reason Entity Framework cannot map a 16 bytes property to Oracle, but any other number will do.
            // ...
        }
    }

The value of the Salt property is typically initialized like this:

Salt = Guid.NewGuid().ToByteArray().Take(15).ToArray();

When we submit changes with a call similar to the following

                    dbContext.BulkSaveChanges(options =>
                    {
                        options.Log = /* logger */;
                    });

...we get the following exception when using an Oracle connection.

NOTES

Exception

Message: An error occurred while updating the entries. See the inner exception for details.
Source:    "Z.EntityFramework.Extensions"    string

Stack: 
   at .Execute()
   at DbContextExtensions.(DbContext this, Boolean , Action`1 , Boolean )
   at DbContextExtensions.BulkSaveChanges(DbContext this, Action`1 bulkOperationFactory)
   at ...

Inner Exception:
Message: Type of value has a mismatch with column type
Couldn't store <System.Byte[]> in SALT Column.  Expected type is Guid.

Stack:
   at System.Data.DataColumn.set_Item(Int32 record, Object value)
   at System.Data.DataRow.set_Item(DataColumn column, Object value)
   at System.Data.DataRow.set_Item(String columnName, Object value)
   at .(List`1 , Object , Dictionary`2 )
   at .Execute()

Further technical details

  <package id="EntityFramework" version="6.1.3" targetFramework="net452" />
  <package id="Oracle.ManagedDataAccess" version="19.6.0" targetFramework="net461" />
  <package id="Oracle.ManagedDataAccess.EntityFramework" version="19.6.0" targetFramework="net461" />
  <package id="Z.EntityFramework.Extensions" version="4.0.96" targetFramework="net461" />
JonathanMagnan commented 4 years ago

Hello @NicolasGauvreau ,

Thank you for reporting, we will look at it and see if something is missing.

Best Regards,

Jon

JonathanMagnan commented 4 years ago

Hello @NicolasGauvreau ,

The v.4.0.100 has been released.

Could you try it and let us know if everything is working perfectly?

Best Regards,

Jon

JonathanMagnan commented 4 years ago

Hello @NicolasGauvreau ,

Since our last conversation, we haven't heard from you.

Did you get the time to try the v.4.0.100?

Don't hesitate to contact us once you give it a try!

Best regards,

Jon

NicolasGauvreau-eaton commented 4 years ago

Sorry for the delay. Yes, it is working properly with v.4.0.100, thanks.

JonathanMagnan commented 4 years ago

Awesome @NicolasGauvreau !

We are glad everything is working out for you!

Don't hesitate to contact us with any questions, issues or feedback.

Best regards,

Jon