コード例 #1
0
        public async Task <int> CreateNewSpec(SpecModel aSpecModel)
        {
            using (var transaction = await context.Database.BeginTransactionAsync())
            {
                var theNewSpecId = await context.Specification.MaxAsync(i => i.SpecId) + 1;

                short theNewRevId = 10; //All new specs start with a revId of 10
                if (aSpecModel.SpecRevModels == null)
                {
                    throw new Exception("Cannot create a new Specification without a Revision.");
                }
                if (aSpecModel.SpecRevModels.Count() > 1)
                {
                    throw new Exception("Cannot save a new Specification with multiple revisions; only one revision is allowed.");
                }
                var theSpecRevModel     = aSpecModel.SpecRevModels.FirstOrDefault(); //Only ONE revision can be passed in.
                var theSubLevelEntities = theSpecRevModel.SubLevels.ToEntities(theNewSpecId, theNewRevId);

                await context.Specification.AddAsync(aSpecModel.ToEntity(theNewSpecId));

                await context.SpecificationRevision.AddAsync(theSpecRevModel.ToEntity(theNewSpecId, theNewRevId));

                await context.SpecSubLevel.AddRangeAsync(theSubLevelEntities);

                foreach (var subLevel in theSpecRevModel.SubLevels)
                {
                    var choicesToAdd = subLevel.Choices.ToEntities(theNewSpecId, theNewRevId).ToList();
                    foreach (var choice in choicesToAdd)
                    {
                        choice.OnlyValidForChoice = null;
                    }
                    await context.AddRangeAsync(choicesToAdd); //Problem is here and it happens immediatly!!! <--- TODO?
                }

                await context.SaveChangesAsync();

                //Updating entities with dependents
                foreach (var subLevel in theSpecRevModel.SubLevels)
                {
                    var originalChoices = subLevel.Choices.ToList();
                    var choicesToUpdate = await context.SpecChoice.Where(i => i.SpecId == theNewSpecId && i.SpecRevId == theNewRevId && i.SubLevelSeqId == subLevel.LevelSeq).ToListAsync();

                    foreach (var choice in choicesToUpdate)
                    {
                        choice.OnlyValidForChoice = originalChoices.FirstOrDefault(i => i.ChoiceSeqId == choice.ChoiceSeqId).OnlyValidForChoiceId;
                    }

                    context.UpdateRange(choicesToUpdate);
                    await context.SaveChangesAsync();
                }


                //Default choice in the subLevel has to be null when saving the data for the first time to prevent an issue with circular dependency.  They must be updated after the first save.
                foreach (var subLevel in theSubLevelEntities)
                {
                    subLevel.DefaultChoice = theSpecRevModel.SubLevels.FirstOrDefault(i => i.LevelSeq == subLevel.SubLevelSeqId).DefaultChoice;
                }

                context.UpdateRange(theSubLevelEntities);

                await context.SaveChangesAsync();

                await transaction.CommitAsync();

                return(theNewSpecId);
            }
        }