What would be the best way to refactor following method?  public void AddOrUpdateDesign(DesignInformation designInformation,                            CommonTransformerProperties commonTransformerProperties,                            SecondaryWindings secondaryWindings,                            CapacitorVoltageDivider capacitorVoltageDivider,                            IntermediateVoltageTransformer intermediateVoltageTransformer,                            SpecialDesignInformation specialDesignInformation,                            TransformerLimits transformerLimits)    {        using (var context = new DesignContext())        {            Design design;            if (!DesignExist(context, designInformation.DesignID))            {                context.DesignInformation.Add(designInformation);                context.CommonTransformerPropeteries.Add(commonTransformerProperties);                context.SecondaryWindings.Add(secondaryWindings);                context.CapacitorVoltageDivider.Add(capacitorVoltageDivider);                context.IntermediateVoltageTransformer.Add(intermediateVoltageTransformer);                context.SpecialDesignInformation.Add(specialDesignInformation);                context.TransformerLimits.Add(transformerLimits);                context.SaveChanges();            }            else            context.Dispose();            {                if (designInformation.DataBaseID ==0)                {                     using (var newcontext = new DesignContext())                 {                    design = GetDesingByNameFromDatabase(newcontext, designInformation.DesignID, designInformation.Technology);                    var existingSecondaryWindings = newcontext.SecondaryWindings.Include(WindingsCollection).First(x => x.DataBaseID == design.DesignInformation.DataBaseID);                    var existingWindingsCollection = existingSecondaryWindings.WindingsCollection.ToList<SecondaryWinding>();                    var updateWindings = secondaryWindings.WindingsCollection.ToList<SecondaryWinding>();                    var addWindings = updateWindings.Except(existingWindingsCollection, wind => wind.SecondaryWindingId);                    var delteWindings = existingWindingsCollection.Except(updateWindings, wind => wind.SecondaryWindingId);                    var modifyWindings = updateWindings.Except(addWindings, wind => wind.SecondaryWindingId);                    foreach (var item in addWindings)                    {                        design.SecondaryWindings.WindingsCollection.Add(item);                    }                    design.SecondaryWindings.WindingsCount = secondaryWindings.WindingsCount;                    delteWindings.ToList<SecondaryWinding>().ForEach(wind => newcontext.Entry(wind).State = System.Data.Entity.EntityState.Deleted);                    foreach (var winding in modifyWindings)                    {                        var existingWindings = design.SecondaryWindings.WindingsCollection.First(x => x.SecondaryWindingId == winding.SecondaryWindingId);                        if (existingWindings != null)                        {                            var windingEntry = newcontext.Entry(existingWindings);                            windingEntry.CurrentValues.SetValues(winding);                        }                    }                    ///special design                    var existingSpecialDesignWire = newcontext.SpecialDesignInformation.Include(SecondaryWindingWireDimensions).First(x => x.DataBaseID == design.DesignInformation.DataBaseID);                    var existingWireCollection = existingSpecialDesignWire.SecondaryWindingWireDimensions.ToList<SpecialDesignSecondaryWire>();                    var updateWires = specialDesignInformation.SecondaryWindingWireDimensions.ToList<SpecialDesignSecondaryWire>();                    var addWire = updateWires.Except(existingWireCollection, wind => wind.SpecialDesignSecondaryWireID);                    var deleteWire = existingWireCollection.Except(updateWires, wind => wind.SpecialDesignSecondaryWireID);                    var modifyWires = updateWires.Except(addWire, wind => wind.SpecialDesignSecondaryWireID);                    foreach (var item in addWire)                    {                        design.SpecialDesignInformation.SecondaryWindingWireDimensions.Add(item);                    }                    deleteWire.ToList<SpecialDesignSecondaryWire>().ForEach(wire => newcontext.Entry(wire).State = System.Data.Entity.EntityState.Deleted);                    foreach (var wire in modifyWires)                    {                        var existingWires = existingWireCollection.Find(x => x.SpecialDesignSecondaryWireID == wire.SpecialDesignSecondaryWireID);                        if (existingWires != null)                        {                            var wireEntry = newcontext.Entry(existingWires);                            wireEntry.CurrentValues.SetValues(wire);                        }                    }                    newcontext.SaveChanges();                }                     int dbId = design.DesignInformation.DataBaseID;                     design.DesignInformation = designInformation;                     design.DesignInformation.DataBaseID = dbId;                     design.CommonTransformerProperties = commonTransformerProperties;                     design.CommonTransformerProperties.DataBaseID = dbId;                     design.SecondaryWindings = secondaryWindings;                     design.SecondaryWindings.DataBaseID = dbId;                     design.CapacitorVoltageDivider = capacitorVoltageDivider;                     design.CapacitorVoltageDivider.DataBaseID = dbId;                     design.IntermediateVoltageTransformer = intermediateVoltageTransformer;                     design.IntermediateVoltageTransformer.DataBaseID = dbId;                     design.SpecialDesignInformation = specialDesignInformation;                     design.SpecialDesignInformation.DataBaseID = dbId;                     using(var newcontext = new DesignContext())                     {                         newcontext.Entry(design.CommonTransformerProperties).State = System.Data.Entity.EntityState.Modified;                         newcontext.Entry(design.SecondaryWindings).State = System.Data.Entity.EntityState.Modified;                         newcontext.Entry(design.CapacitorVoltageDivider).State = System.Data.Entity.EntityState.Modified;                         newcontext.Entry(design.IntermediateVoltageTransformer).State = System.Data.Entity.EntityState.Modified;                         newcontext.Entry(design.SpecialDesignInformation).State = System.Data.Entity.EntityState.Modified;                         newcontext.Entry(design.DesignInformation).State = System.Data.Entity.EntityState.Modified;                         newcontext.SaveChanges();                         return;                     }                }                }                context.Dispose();                using (var newcontext = new DesignContext())                 {                    design = GetDesingByNameFromDatabase(newcontext, designInformation.DesignID, designInformation.Technology);                    var existingSecondaryWindings = newcontext.SecondaryWindings.Include(WindingsCollection).First(x => x.DataBaseID == designInformation.DataBaseID);                    var existingWindingsCollection = existingSecondaryWindings.WindingsCollection.ToList<SecondaryWinding>();                    var updateWindings = secondaryWindings.WindingsCollection.ToList<SecondaryWinding>();                    var addWindings = updateWindings.Except(existingWindingsCollection, wind => wind.SecondaryWindingId);                    var delteWindings = existingWindingsCollection.Except(updateWindings, wind => wind.SecondaryWindingId);                    var modifyWindings = updateWindings.Except(addWindings, wind => wind.SecondaryWindingId);                    foreach (var item in addWindings)                    {                        design.SecondaryWindings.WindingsCollection.Add(item);                    }                    design.SecondaryWindings.WindingsCount = secondaryWindings.WindingsCount;                    delteWindings.ToList<SecondaryWinding>().ForEach(wind => newcontext.Entry(wind).State = System.Data.Entity.EntityState.Deleted);                    foreach (var winding in modifyWindings)                    {                        var existingWindings = design.SecondaryWindings.WindingsCollection.First(x => x.SecondaryWindingId == winding.SecondaryWindingId);                        if (existingWindings != null)                        {                            var windingEntry = newcontext.Entry(existingWindings);                            windingEntry.CurrentValues.SetValues(winding);                        }                    }                    ///special design                    var existingSpecialDesignWire = newcontext.SpecialDesignInformation.Include(SecondaryWindingWireDimensions).First(x => x.DataBaseID == designInformation.DataBaseID);                    var existingWireCollection = existingSpecialDesignWire.SecondaryWindingWireDimensions.ToList<SpecialDesignSecondaryWire>();                    var updateWires = specialDesignInformation.SecondaryWindingWireDimensions.ToList<SpecialDesignSecondaryWire>();                    var addWire = updateWires.Except(existingWireCollection, wind => wind.SpecialDesignSecondaryWireID);                    var deleteWire = existingWireCollection.Except(updateWires, wind => wind.SpecialDesignSecondaryWireID);                    var modifyWires = updateWires.Except(addWire, wind => wind.SpecialDesignSecondaryWireID);                    foreach (var item in addWire)                    {                        design.SpecialDesignInformation.SecondaryWindingWireDimensions.Add(item);                    }                    deleteWire.ToList<SpecialDesignSecondaryWire>().ForEach(wire => newcontext.Entry(wire).State = System.Data.Entity.EntityState.Deleted);                    foreach (var wire in modifyWires)                    {                        var existingWires = existingWireCollection.Find(x => x.SpecialDesignSecondaryWireID == wire.SpecialDesignSecondaryWireID);                        if (existingWires != null)                        {                            var wireEntry = newcontext.Entry(existingWires);                            wireEntry.CurrentValues.SetValues(wire);                        }                    }                    newcontext.SaveChanges();                }                design.DesignInformation = designInformation;                design.CommonTransformerProperties = commonTransformerProperties;                design.SecondaryWindings = secondaryWindings;                design.CapacitorVoltageDivider = capacitorVoltageDivider;                design.IntermediateVoltageTransformer = intermediateVoltageTransformer;                design.SpecialDesignInformation = specialDesignInformation;                using (var newcontext = new DesignContext())                {                    newcontext.Entry(design.DesignInformation).State = System.Data.Entity.EntityState.Modified;                    newcontext.Entry(design.CommonTransformerProperties).State = System.Data.Entity.EntityState.Modified;                    newcontext.Entry(design.SecondaryWindings).State = System.Data.Entity.EntityState.Modified;                    newcontext.Entry(design.CapacitorVoltageDivider).State = System.Data.Entity.EntityState.Modified;                    newcontext.Entry(design.IntermediateVoltageTransformer).State = System.Data.Entity.EntityState.Modified;                    newcontext.Entry(design.SpecialDesignInformation).State = System.Data.Entity.EntityState.Modified;                    newcontext.SaveChanges();                }                }                }
Saving or updating to databse
c#;.net;entity framework
Too much copy-paste, use private methods for code allocation. For example private void UpdateSecondaryWindings(Design design, DesignContext newcontext, DesignInformation designInformation) {    var existingSecondaryWindings = newcontext.SecondaryWindings.Include(WindingsCollection).First(x => x.DataBaseID == design.DesignInformation.DataBaseID);    var existingWindingsCollection = existingSecondaryWindings.WindingsCollection.ToList<SecondaryWinding>();    var updateWindings = secondaryWindings.WindingsCollection.ToList<SecondaryWinding>();    var addWindings = updateWindings.Except(existingWindingsCollection, wind => wind.SecondaryWindingId);    var delteWindings = existingWindingsCollection.Except(updateWindings, wind => wind.SecondaryWindingId);    var modifyWindings = updateWindings.Except(addWindings, wind => wind.SecondaryWindingId);    foreach (var item in addWindings)    {        design.SecondaryWindings.WindingsCollection.Add(item);    }    design.SecondaryWindings.WindingsCount = secondaryWindings.WindingsCount;    delteWindings.ToList<SecondaryWinding>().ForEach(wind => newcontext.Entry(wind).State = System.Data.Entity.EntityState.Deleted);    foreach (var winding in modifyWindings)    {        var existingWindings = design.SecondaryWindings.WindingsCollection.First(x => x.SecondaryWindingId == winding.SecondaryWindingId);        if (existingWindings != null)        {            var windingEntry = newcontext.Entry(existingWindings);            windingEntry.CurrentValues.SetValues(winding);        }    }}When you have function or procedure with 4 or more parameters you can create class and pass instance. Example, you can create DesignProperties class internal class DesignProperties {DesignInformation DesignInformation {get;set;}CommonTransformerProperties CommonTransformerProperties {get;set;}SecondaryWindings SecondaryWindings {get;set;}CapacitorVoltageDivider CapacitorVoltageDivider {get;set;}IntermediateVoltageTransformer IntermediateVoltageTransformer {get;set;}SpecialDesignInformation SpecialDesignInformation  {get;set;}TransformerLimits TransformerLimits {get;set;}}And then public void AddOrUpdateDesign(DesignProperties properties){    ...}Your DesignContext also can have method Add with param DesignProperties and calls like thiscontext.Add(designProperties)and your Design class can exist method Update public class Design{    ...    public void Update(DesignProperties properties){    }}If you want to update it with DataBaseID add procedure with bool IsDbIdUpdatepublic void Update(DesignProperties props, int dbId)and SaveChanges() should calls inside this method.Next one    else    context.Dispose();    {I dont understand why you put brackets after dispose method, because code inside always work, also !DesignExist(context, designInformation.DesignID) return true. You can remove this brackets.Add to DesignContext function like SetState() and newContext.SetState(design, System.Data.Entity.EntityState.Modified);After my edit code should be looks like thispublic void AddOrUpdateDesign(DesignProperties designProperties)    {        Design design;        using (var context = new DesignContext())        {            if (!DesignExist(context, designInformation.DesignID))                context.Add(designProperties)        }        using (var context = new DesignContext())         {            design = GetDesingByNameFromDatabase(context, designInformation.DesignID, designInformation.Technology);            UpdateSecondaryWindings(design, context, designInformation);            UpdateWires(design, context, designInformation);            context.SaveChanges();        }        if (designInformation.DataBaseID == 0)        {            int dbId = design.DesignInformation.DataBaseID;            design.Update(designProperties, dbId);        }        else        {            design.Update(designProperties);        }        using(var context = new DesignContext())        {            context.SetState(design, System.Data.Entity.EntityState.Modified);            context.SaveChanges();        }    }And some private methods ) 