Cannot insert "subclass" object into database using EF Core code first

0

I have two classes:

Workout.cs:

public class Workout
        {
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public ICollection<WorkoutSet> Sets { get; set; }
    
            public DateTimeOffset FromDateTime { get; set; }
            
            public DateTimeOffset ToDateTime { get; set; }
        }

and I have subclass

WorkoutSet.cs
    
public class WorkoutSet
        {
            public int Id { get; set; }
    
            public int WorkoutId { get; set; }
            
            public Workout Workout { get; set; }
    
            public int OrderNumber { get; set; }
    
            public int Type { get; set; }
    
            public int Duration { get; set; }
        }

This setup makes WorkoutSet have a foreign key to Workout. This is great but I have an issue inserting data into the database. I am trying to insert a new workout with new workout sets (like the definition of a workout) with following code:

using (IDbContextTransaction transaction = await _dbContext.Database.BeginTransactionAsync())
                {
                    try
                    {
                        await _dbContext.Workouts.AddAsync(data);
                        await _dbContext.SaveChangesAsync();
                        await _dbContext.WorkoutSets.AddRangeAsync(data.Sets);
                        await _dbContext.SaveChangesAsync();
                        await transaction.CommitAsync();
                        return data;
                    }
                    catch (Exception e)
                    {
                        await transaction.RollbackAsync();
                        throw new Exception("Cannot create workout");
                    }
                }

When I am debugging the application, I see that proper ID is created for a workout, I see that proper newly generated ID is created for each workoutset, but after workoutset when I call savechangesasync I receive error:

Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert explicit value for identity column in table 'WorkoutSets' when IDENTITY_INSERT is set to OFF.

My database context looks like this:

public class TrainingDbContext : DbContext
    {
        public TrainingDbContext(DbContextOptions<TrainingDbContext> options) : base (options)
        {
        }

        public DbSet<Workout> Workouts { get; set; }

        public DbSet<WorkoutSet> WorkoutSets { get; set; }
    }

I was looking into different solutions found online like adding attributes like this to ID :

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]

Or overriding OnModelCreating for WorkoutSet entity but no success. Every solution I found could not resolve my problem. I understand that issue is in EF Core but I am not 100% sure how to resolve it.

asp.net-core
entity-framework-core
ef-code-first
asp.net-core-3.1
asked on Stack Overflow Dec 11, 2020 by zuboje • edited Dec 11, 2020 by mj1313

1 Answer

1

Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert explicit value for identity column in table 'WorkoutSets' when IDENTITY_INSERT is set to OFF.

Seems you're inserting values for Id that is an identity column.

You can try not to assign value to Id property in the Workout and WorkoutSet to make it self increasing.

answered on Stack Overflow Dec 11, 2020 by mj1313

User contributions licensed under CC BY-SA 3.0