Skip to content

Bug: Unable to insert object when primary key includes an enum (unable to map enum to int) #1203

Open
@premell

Description

@premell

Bug Description

I try to insert an object into my database. It succesfully inserts the row but then it crashes immediately afterwards when it tries to return the primary key.
The reason is that one of the columns of the primary key is an enum and it fails to map it from int in the database to enum in c#:

connection.Insert(metatabell);

I have managed to work around this temporarily by only returning the identity which is null in my case

GlobalConfiguration
    .Setup(new GlobalConfigurationOptions()
    {
        KeyColumnReturnBehavior = KeyColumnReturnBehavior.Identity
    })
    .UseSqlServer();

Exception Message:

System.InvalidCastException: Invalid cast from 'System.Int32' to 'Common.MetatabellType'.
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at RepoDb.Converter.ToType[T](Object value)
   at lambda_method7(Closure, Object, Object)
   at RepoDb.DbConnectionExtension.InsertInternalBase[TEntity,TResult](IDbConnection connection, String tableName, TEntity entity, IEnumerable`1 fields, String hints, Nullable`1 commandTimeout, String traceKey, IDbTransaction transaction, ITrace trace, IStatementBuilder statementBuilder)
   at RepoDb.DbConnectionExtension.InsertInternal[TEntity,TResult](IDbConnection connection, String tableName, TEntity entity, IEnumerable`1 fields, String hints, Nullable`1 commandTimeout, String traceKey, IDbTransaction transaction, ITrace trace, IStatementBuilder statementBuilder)
   at RepoDb.DbConnectionExtension.Insert[TEntity](IDbConnection connection, TEntity entity, IEnumerable`1 fields, String hints, Nullable`1 commandTimeout, String traceKey, IDbTransaction transaction, ITrace trace, IStatementBuilder statementBuilder)

Schema and Model:

    public class Metatabell
    {
        public DateTime uploadedDate { get; set; }
        public MetatabellType type { get; set; }
        public string comment { get; set; }
        public int version { get; set; }
        public byte[] fileContent { get; set; }
}

I use entity framework for generating the database and created the primary key from the type and the version:

        modelBuilder.Entity<Dto.Metatabell>().HasKey(a =>
            new
            {
                a.type, a.version
            });

This is what it looks like in the database:

Image

Library Version:

Example: RepoDb v1.13.2-alpha1 and RepoDb.SqlServer v1.13.2-alpha1

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions