Skip to content

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

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

Open
premell opened this issue Apr 25, 2025 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@premell
Copy link

premell commented Apr 25, 2025

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

@premell premell added the bug Something isn't working label Apr 25, 2025
@premell
Copy link
Author

premell commented Apr 25, 2025

btw this is an awesome library :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants