everyone, sorry to bother you, hopefully someone can answer. I've been working on this project for a week now and I've come across a very weird bug to me and I can't wrap my mind around why it won't work. The thing is that it actually works just fine, but doesn't in one case scenario.

So it's an app like Spotify, I have Records (music) and Genres and it's a many-to-many relationship (created automatically by entity framework core convention of putting ICollection on both classes). This is the code in controller:

// this just gets the record with specified id and includes genres, tested and working
var record = await _unitOfWork.Records.Get(x => x.Id == id, new List<string> { "Genres" });
            // checking if record is null
            // mapping view model (dto) to the record from db to capture any changes on record itself
            _mapper.Map(recordDto, record);
            record.Genres ??= new List<Genre>();

            foreach (var currentGenre in record.Genres)
            {
                // Attach() really only attaches
                _unitOfWork.Genres.Attach(currentGenre);
                if (!recordDto.GenresIds.Contains(currentGenre.Id))
                {
                    record.Genres.Remove(currentGenre);
                }
            }

            foreach (var genreId in recordDto.GenresIds)
            {
                var genre = await _unitOfWork.Genres.GetById(genreId);
                // check if null
                _unitOfWork.Genres.Attach(genre);
                record.Genres.Add(genre);
            }

            //update function attaches the record and uses EntityState.Modified
            _unitOfWork.Records.Update(record);
            // save is just calling SaveChangesAsync() on the dbcontext
            await _unitOfWork.Save();

            return NoContent();

The bug is that when there are no relationship between Records and Genres in the database, it does nothing, no matter what IDs i put in a json array. but when there is at least one reliotionship in the junction table (the record must have at least one genre already), it works perfectly, tested on all cases. Insert works just fine, though. adds the record and relations with no problems. if you need more code, let me know! literally first time i had to ask for help, always tryna fix things myself. I could solve it by not allowing genres for records to be empty, but I really wanna know the issue, cause one time that wont be the option. btw no errors, it jus tdoesn't work. I tested in debug mode with a breakpoint, it adds the genres nicely to the record object, but it just doesn't create relationships in the db. If anyone tries helping, thank you!!

EDIT: still wanna know what the issue is, but I'll probably end up just creating the junction table(s) myself to have more control. Should also be better on performance as I won't need to load any genres from db. It's just that I have to create a lot of them and wanted to save time.

0

There are 0 best solutions below