コード例 #1
0
        public async Task ImportForeignBayTypes(WRLDCWarehouseDbContext _context, ILogger _log, string oracleConnStr, EntityWriteOption opt)
        {
            BayTypeExtract bayTypeExtract = new BayTypeExtract();
            List <BayType> bayTypes       = bayTypeExtract.ExtractBays(oracleConnStr);

            LoadBayType loadBayType = new LoadBayType();

            foreach (BayType bayType in bayTypes)
            {
                BayType insertedBayType = await loadBayType.LoadSingleAsync(_context, _log, bayType, opt);
            }
        }
コード例 #2
0
        /***************************************************/

        private static SeatingBlockType DetermineBlockToCopy(BayType current, BayType next, StadiaType bowlType, int cornerCount)
        {
            SeatingBlockType bt = SeatingBlockType.Side;

            if (bowlType == StadiaType.EightArc || bowlType == StadiaType.Orthogonal)
            {
                if (current == 0 && next == 0)
                {
                    bt = SeatingBlockType.Side;                           //side standard
                }
                if (current == BayType.End && next == BayType.End)
                {
                    bt = SeatingBlockType.End;                                               //end standard
                }
                if (current == BayType.Corner && next == BayType.Corner)
                {
                    bt = SeatingBlockType.CornerNoVom;                                                     //corner standard
                }
                //corner vom or no vom should be determined by number of seats on last row 14 either side of vomitory max
                if (current == BayType.Corner && cornerCount % 2 == 0)
                {
                    bt = SeatingBlockType.Corner;                                                   //corner vomitory standard
                }
                if (current == 0 && next == BayType.Corner)
                {
                    bt = SeatingBlockType.Transition1;                                        //side to corner transition
                }
                if (current == BayType.Corner && next == BayType.End)
                {
                    bt = SeatingBlockType.Transition2;                                                  //corner to end transition
                }
                if (current == BayType.End && next == BayType.Corner)
                {
                    bt = SeatingBlockType.Transition2mirrored;                                                  //end to corner transition
                }
                if (current == BayType.Corner && next == 0)
                {
                    bt = SeatingBlockType.Transition1mirrored;                                        //corner to sidetransition
                }
            }
            if (bowlType == StadiaType.NoCorners)
            {
                if (current == BayType.End)
                {
                    bt = SeatingBlockType.End;                        //end standard otherwise a side is returned
                }
            }
            //if bowlType is circular side is returned
            return(bt);
        }
コード例 #3
0
        /***************************************************/
        /**** Private Methods                           ****/
        /***************************************************/

        private static void CircularPlaneSetUp(ref TheatronPlan plan, double radius, double structBayW)
        {
            plan.SectionOrigins = new List <ProfileOrigin>();

            int    numBays      = (int)(Math.Floor(Math.PI * radius * 2 / structBayW));
            double theta        = 2 * Math.PI / numBays;
            bool   halfbayStart = false;

            plan.SectionOrigins = ArcSweepBay(0, 0, theta, 0, radius, numBays, halfbayStart, 1.0);
            BayType bayType = BayType.Side;

            for (int i = 0; i < plan.SectionOrigins.Count; i++)
            {
                plan.StructBayType.Add(bayType);
            }
        }
コード例 #4
0
        public List <BayType> ExtractBays(string oracleConnString)
        {
            using (OracleConnection con = new OracleConnection(oracleConnString))
            {
                using (OracleCommand cmd = con.CreateCommand())
                {
                    try
                    {
                        con.Open();
                        cmd.BindByName = true;

                        cmd.CommandText = "select ID, TYPE from REPORTING_WEB_UI_UAT.BAY_TYPE where :id=1";

                        // Assign id parameter
                        OracleParameter id = new OracleParameter("id", 1);
                        cmd.Parameters.Add(id);

                        //Execute the command and use DataReader to display the data
                        OracleDataReader reader = cmd.ExecuteReader();

                        List <BayType> bayTypes = new List <BayType>();
                        while (reader.Read())
                        {
                            BayType bayType = new BayType();
                            bayType.WebUatId = reader.GetInt32(0);
                            bayType.Name     = reader.GetString(1);
                            bayTypes.Add(bayType);
                        }

                        reader.Dispose();

                        return(bayTypes);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return(null);
                    }
                }
            }
        }
コード例 #5
0
        public async Task <BayType> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, BayType bayType, EntityWriteOption opt)
        {
            // check if entity already exists
            BayType existingBayType = await _context.BayTypes.SingleOrDefaultAsync(bt => bt.WebUatId == bayType.WebUatId);

            // check if we have to replace the entity completely
            if (opt == EntityWriteOption.Replace && existingBayType != null)
            {
                _context.BayTypes.Remove(existingBayType);
            }

            // check if entity is not present or check if we have to replace the entity completely
            if (existingBayType == null || (opt == EntityWriteOption.Replace && existingBayType != null))
            {
                _context.BayTypes.Add(bayType);
                await _context.SaveChangesAsync();

                return(bayType);
            }

            // check if we should not modify existing entities
            if (opt == EntityWriteOption.DontReplace && existingBayType != null)
            {
                return(existingBayType);
            }

            // check if we have to modify the entity
            if (opt == EntityWriteOption.Modify && existingBayType != null)
            {
                existingBayType.Name = bayType.Name;
                await _context.SaveChangesAsync();

                return(existingBayType);
            }
            return(null);
        }
コード例 #6
0
        /***************************************************/
        /**** Private Methods                           ****/
        /***************************************************/
        private static void NoCornerPlanSetUp(ref TheatronPlan plan, double length, double width, double endBound, double structBayW, double sideBound)
        {
            int nSideBays = (int)(Math.Floor(((length + endBound) / 2) / structBayW) * 2);
            int nEndBays  = (int)(Math.Floor(((width + sideBound) / 2) / structBayW) * 2);

            plan.SectionOrigins = new List <ProfileOrigin>();

            double actualBayW;
            double xMin;
            double yMin;
            double oX, oY, dX, dY;
            int    count  = 0;
            Point  origin = new Point();
            Vector xdir   = new Vector();

            ProfileOrigin tempPlane = new ProfileOrigin();
            BayType       bayType   = BayType.Side;//0 = side, 1= end, 2 =corner

            for (int i = 0; i < 4; i++)
            {
                if (i % 2 == 0)//side bay
                {
                    bayType    = 0;
                    actualBayW = (length + endBound) / nSideBays;
                    for (int d = 0; d <= nSideBays; d++)
                    {
                        plan.StructBayType.Add(bayType);
                        if (i == 0)//right side
                        {
                            yMin = (nSideBays * actualBayW) / -2;
                            //origin xyz
                            oX = ((width + sideBound) / 2);
                            oY = yMin + (actualBayW * d);
                            dX = 1;
                            dY = 0;
                        }
                        else//left side
                        {
                            yMin = (nSideBays * actualBayW) / 2;
                            //origin xyz
                            oX = -((width + sideBound) / 2);
                            oY = yMin - (actualBayW * d);
                            dX = -1;
                            dY = 0;
                        }
                        origin    = Geometry.Create.Point(oX, oY, 0);
                        xdir      = Geometry.Create.Vector(dX, dY, 0);
                        tempPlane = Create.ProfileOrigin(origin, xdir);
                        plan.SectionOrigins.Add(tempPlane);
                        count++;
                    }
                }

                else
                {
                    bayType    = BayType.End;
                    actualBayW = (width + sideBound) / nEndBays;
                    xMin       = (nEndBays * actualBayW) / 2;
                    //
                    for (int d = 0; d <= nEndBays; d++)
                    {
                        plan.StructBayType.Add(bayType);
                        if (i == 1)// northEnd
                        {
                            //origin xyz
                            oX = xMin - (actualBayW * d);
                            oY = ((length + endBound) / 2);

                            dX = 0;
                            dY = 1;// 0;
                        }
                        else
                        {
                            //origin xyz
                            oX = -xMin + (actualBayW * d);
                            oY = -((length + endBound) / 2);
                            dX = 0;
                            dY = -1;
                        }
                        origin    = Geometry.Create.Point(oX, oY, 0);
                        xdir      = Geometry.Create.Vector(dX, dY, 0);
                        tempPlane = Create.ProfileOrigin(origin, xdir);
                        plan.SectionOrigins.Add(tempPlane);
                        count++;
                    }
                }
            }
        }
コード例 #7
0
        /***************************************************/
        /**** Private Methods                           ****/
        /***************************************************/
        private static void RadialPlanSetUp(ref TheatronPlan plan, double length, double width, double sideBound, double sideRadius, double endBound, double endRadius, double cornerR, int nCornerBays, double structBayW, double cornerFraction)
        {
            plan.SectionOrigins = new List <ProfileOrigin>();

            int    count       = 0;
            double sidecentreX = width / 2 + sideBound - sideRadius;
            double sidecentreY = 0;
            double endcentreX  = 0;
            double endcentreY  = length / 2 + endBound - endRadius;
            Point  intersect   = IntersectCircles(sidecentreX, sidecentreY, sideRadius - cornerR, endcentreX, endcentreY, endRadius - cornerR);
            double centreX     = intersect.X;
            double centreY     = intersect.Y;

            double sweepAngleSide = 2 * Math.Atan(centreY / (centreX - sidecentreX));
            double sweepSideBay   = 2 * Math.Asin(structBayW / 2 / sideRadius);

            int nSideBays = (int)(Math.Floor(sweepAngleSide / sweepSideBay));

            double sweepAngleEnd = 2 * Math.Atan(centreX / (centreY - endcentreY));
            double sweepEndBay   = 2 * Math.Asin(structBayW / 2 / endRadius);
            int    nEndBays      = (int)(Math.Floor(sweepAngleEnd / sweepEndBay));


            Vector endArc  = Geometry.Create.Vector(centreX, centreY - endcentreY, 0);
            Vector sideArc = Geometry.Create.Vector(centreX - sidecentreX, centreY, 0);

            double  cornerSweep = Geometry.Query.Angle(sideArc, endArc);//*Math.PI/180;
            double  cornA = cornerSweep / (nCornerBays + 2 * cornerFraction);
            double  trueR = cornerR / Math.Cos(cornA / 2);
            double  theta = 1, startAngle = 1, radius = 1;
            int     numBays          = 1;
            bool    fractionbayStart = false;
            BayType bayType          = BayType.Side; //0 = side, 1= end, 2 =corner

            for (int i = 0; i < 8; i++)
            {
                switch (i)
                {
                case 0:    //side right
                    centreX          = sidecentreX;
                    centreY          = 0;
                    theta            = sweepSideBay;
                    numBays          = nSideBays;
                    startAngle       = (numBays * theta) / -2;
                    radius           = sideRadius;
                    fractionbayStart = false;
                    bayType          = BayType.Side;
                    break;

                case 1:    //top right corner
                    centreX          = intersect.X;
                    centreY          = intersect.Y;
                    theta            = cornA;
                    startAngle       = sweepAngleSide / 2;
                    radius           = cornerR;
                    numBays          = nCornerBays;
                    fractionbayStart = true;
                    bayType          = BayType.Corner;
                    break;

                case 2:    //north end
                    centreX          = 0;
                    centreY          = endcentreY;
                    theta            = sweepEndBay;
                    numBays          = nEndBays;
                    startAngle       = Math.PI / 2 - numBays * theta / 2;
                    radius           = endRadius;
                    fractionbayStart = false;
                    bayType          = BayType.End;
                    break;

                case 3:    //north west corner
                    centreX          = -intersect.X;
                    centreY          = intersect.Y;
                    theta            = cornA;
                    startAngle       = Math.PI / 2 + sweepAngleEnd / 2;
                    radius           = cornerR;
                    numBays          = nCornerBays;
                    fractionbayStart = true;
                    bayType          = BayType.Corner;
                    break;

                case 4:    //west side
                    centreX          = -sidecentreX;
                    centreY          = 0;
                    theta            = sweepSideBay;
                    numBays          = nSideBays;
                    startAngle       = Math.PI - (numBays * theta) / 2;
                    radius           = sideRadius;
                    fractionbayStart = false;
                    bayType          = BayType.Side;
                    break;

                case 5:    //south west conrer
                    centreX          = -intersect.X;
                    centreY          = -intersect.Y;
                    theta            = cornA;
                    startAngle       = Math.PI + sweepAngleSide / 2;
                    radius           = cornerR;
                    numBays          = nCornerBays;
                    fractionbayStart = true;
                    bayType          = BayType.Corner;
                    break;

                case 6:    // south end
                    centreX          = 0;
                    centreY          = -endcentreY;
                    theta            = sweepEndBay;
                    numBays          = nEndBays;
                    startAngle       = 1.5 * Math.PI - numBays * theta / 2;
                    radius           = endRadius;
                    fractionbayStart = false;
                    bayType          = BayType.End;
                    break;

                case 7:    //south east corner
                    centreX          = intersect.X;
                    centreY          = -intersect.Y;
                    theta            = cornA;
                    startAngle       = 1.5 * Math.PI + sweepAngleEnd / 2;
                    radius           = cornerR;
                    numBays          = nCornerBays;
                    fractionbayStart = true;
                    bayType          = BayType.Corner;
                    break;
                }
                List <ProfileOrigin> partPlanes = ArcSweepBay(centreX, centreY, theta, startAngle, radius, numBays, fractionbayStart, cornerFraction);
                for (int p = 0; p < partPlanes.Count; p++)
                {
                    plan.SectionOrigins.Add(partPlanes[p]);

                    plan.StructBayType.Add(bayType);
                    count++;
                }
            }
        }
コード例 #8
0
        /***************************************************/
        /**** Private Methods                           ****/
        /***************************************************/
        private static void orthoPlanSetUp(ref TheatronPlan plan, double length, double width, double endBound, double sideBound, double cornerR, double structBayW, int nCornerBays)
        {
            int nSideBays = (int)(Math.Floor(((length + endBound) / 2 - cornerR) / structBayW) * 2);
            int nEndBays  = (int)(Math.Floor(((width + sideBound) / 2 - cornerR) / structBayW) * 2);

            plan.SectionOrigins = new List <ProfileOrigin>();

            double        cornA = Math.PI / 2 / (nCornerBays + 1);
            double        trueR = cornerR / Math.Cos(cornA / 2);
            double        xMin;
            double        yMin;
            double        oX, oY, dX, dY;
            int           count      = 0;
            Point         origin     = new Point();
            Vector        xdir       = new Vector();
            Vector        ydir       = Vector.ZAxis;
            Vector        normal     = new Vector();
            ProfileOrigin tempOrigin = new ProfileOrigin();
            BayType       bayType    = 0; //0 = side, 1= end, 2 =corner

            for (int i = 0; i < 8; i++)
            {
                if (i == 0 || i == 4)//side bay
                {
                    bayType = 0;
                    for (int d = 0; d <= nSideBays; d++)
                    {
                        plan.StructBayType.Add(bayType);
                        if (i == 0)//right side
                        {
                            yMin = (nSideBays * structBayW) / -2;
                            //origin xyz
                            oX = ((width + sideBound) / 2);
                            oY = yMin + (structBayW * d);
                            dX = 1;
                            dY = 0;
                        }
                        else//left side
                        {
                            yMin = (nSideBays * structBayW) / 2;
                            //origin xyz
                            oX = -((width + sideBound) / 2);
                            oY = yMin - (structBayW * d);
                            dX = -1;
                            dY = 0;
                        }
                        origin     = Geometry.Create.Point(oX, oY, 0);
                        xdir       = Geometry.Create.Vector(dX, dY, 0);
                        tempOrigin = Create.ProfileOrigin(origin, xdir);
                        plan.SectionOrigins.Add(tempOrigin);
                        count++;
                    }
                }
                else
                {
                    if (i == 2 || i == 6)//end bay
                    {
                        bayType = BayType.End;
                        xMin    = (nEndBays * structBayW) / 2;
                        //
                        for (int d = 0; d <= nEndBays; d++)
                        {
                            plan.StructBayType.Add(bayType);
                            if (i == 2)// northEnd
                            {
                                //origin xyz
                                oX = xMin - (structBayW * d);
                                oY = ((length + endBound) / 2);
                                dX = 0;
                                dY = 1;
                            }
                            else
                            {
                                //origin xyz
                                oX = -xMin + (structBayW * d);
                                oY = -((length + endBound) / 2);

                                dX = 0;
                                dY = -1;
                            }
                            origin     = Geometry.Create.Point(oX, oY, 0);
                            xdir       = Geometry.Create.Vector(dX, dY, 0);
                            tempOrigin = Create.ProfileOrigin(origin, xdir);
                            plan.SectionOrigins.Add(tempOrigin);
                            count++;
                        }
                    }
                    else//corners
                    {
                        //local centres cs at fillets
                        bayType = BayType.Corner;
                        double centreX    = (width + sideBound) / 2 - cornerR;
                        double centreY    = (length + endBound) / 2 - cornerR;
                        double startAngle = 0;
                        if (i == 1) //NE++
                        {
                        }
                        if (i == 3) //NW-+
                        {
                            centreX    = -centreX;
                            startAngle = Math.PI / 2;
                        }
                        if (i == 5) //SW--
                        {
                            centreY    = -centreY;
                            centreX    = -centreX;
                            startAngle = Math.PI;
                        }
                        if (i == 7) //SE+-
                        {
                            centreY    = -1 * centreY;
                            startAngle = Math.PI * 1.5;
                        }

                        //
                        for (int d = 0; d <= nCornerBays; d++)
                        {
                            plan.StructBayType.Add(bayType);
                            if (d == 0)//half bay on first
                            {
                                oX = centreX + trueR * Math.Cos(startAngle + cornA / 2);
                                oY = centreY + trueR * Math.Sin(startAngle + cornA / 2);
                                dX = trueR * Math.Cos(startAngle + cornA / 2);
                                dY = trueR * Math.Sin(startAngle + cornA / 2);
                            }
                            else
                            {
                                oX = centreX + trueR * Math.Cos(startAngle + (cornA * d + cornA / 2));
                                oY = centreY + trueR * Math.Sin(startAngle + (cornA * d + cornA / 2));
                                dX = trueR * Math.Cos(startAngle + (cornA * d + cornA / 2));
                                dY = trueR * Math.Sin(startAngle + (cornA * d + cornA / 2));
                            }
                            origin     = Geometry.Create.Point(oX, oY, 0);
                            xdir       = Geometry.Create.Vector(dX, dY, 0);
                            tempOrigin = Create.ProfileOrigin(origin, xdir);
                            plan.SectionOrigins.Add(tempOrigin);
                            count++;
                        }
                    }
                }
            }
        }
コード例 #9
0
        public async Task <Bay> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, BayForeign bayForeign, EntityWriteOption opt)
        {
            // check if entity already exists
            Bay existingBay = await _context.Bays.SingleOrDefaultAsync(b => b.WebUatId == bayForeign.WebUatId);

            // check if we should not modify existing entities
            if (opt == EntityWriteOption.DontReplace && existingBay != null)
            {
                return(existingBay);
            }

            // find the BayType via the BayTypeWebUatId
            int     bayTypeWebUatId = bayForeign.BayTypeWebUatId;
            BayType bayType         = await _context.BayTypes.SingleOrDefaultAsync(ct => ct.WebUatId == bayTypeWebUatId);

            // if BayType doesnot exist, skip the import. Ideally, there should not be such case
            if (bayType == null)
            {
                _log.LogCritical($"Unable to find BayType with webUatId {bayTypeWebUatId} while inserting Bay with webUatId {bayForeign.WebUatId} and name {bayForeign.Name}");
                return(null);
            }

            // find the VoltLevel via the VoltLevelWebUatId
            int       voltWebUatId = bayForeign.VoltageWebUatId;
            VoltLevel voltLevel    = await _context.VoltLevels.SingleOrDefaultAsync(v => v.WebUatId == voltWebUatId);

            // if VoltLevel doesnot exist, skip the import. Ideally, there should not be such case
            if (voltLevel == null)
            {
                _log.LogCritical($"Unable to find voltLevel with webUatId {voltWebUatId} while inserting Bay with webUatId {bayForeign.WebUatId} and name {bayForeign.Name}");
                return(null);
            }

            // find the Substation via the SubstationWebUatId
            int        substationWebUatId = bayForeign.SubstationWebUatId;
            Substation substation         = await _context.Substations.SingleOrDefaultAsync(ss => ss.WebUatId == substationWebUatId);

            // if Substation doesnot exist, skip the import. Ideally, there should not be such case
            if (substation == null)
            {
                _log.LogCritical($"Unable to find Substation with webUatId {substationWebUatId} while inserting Bay with webUatId {bayForeign.WebUatId} and name {bayForeign.Name}");
                return(null);
            }

            async Task <int> getElementId(int entWebUatId, string entType)
            {
                // find the sourceEntityId of the Bay
                int entityId = -1;

                try
                {
                    // entityType can be AC LINE CIRCUIT,TCSC,BUS REACTOR,TRANSFORMER,Filter Bank,BUS,FSC,LINE REACTOR
                    string entityType      = bayForeign.SourceEntityType;
                    int    elementWebUatId = bayForeign.SourceEntityWebUatId;
                    if (entityType == "AC LINE CIRCUIT")
                    {
                        // attach element is AcTransLineCkt
                        entityId = (await _context.AcTransLineCkts.SingleOrDefaultAsync(ckt => ckt.WebUatId == elementWebUatId)).AcTransLineCktId;
                    }
                    else if (entityType == "TCSC")
                    {
                        // attach element is Compensator
                        entityId = (await _context.Compensators.SingleOrDefaultAsync(c => c.WebUatId == elementWebUatId)).CompensatorId;
                    }
                    else if (entityType == "BUS REACTOR")
                    {
                        // attach element is BUS REACTOR
                        entityId = (await _context.BusReactors.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).BusReactorId;
                    }
                    else if (entityType == "TRANSFORMER")
                    {
                        // attach element is TRANSFORMER
                        entityId = (await _context.Transformers.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).TransformerId;
                    }
                    else if (entityType == "Filter Bank")
                    {
                        // attach element is Filter Bank
                        entityId = (await _context.FilterBanks.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).FilterBankId;
                    }
                    else if (entityType == "BUS")
                    {
                        // attach element is BUS
                        entityId = (await _context.Buses.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).BusId;
                    }
                    else if (entityType == "FSC")
                    {
                        // attach element is FSC
                        entityId = (await _context.Fscs.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).FscId;
                    }
                    else if (entityType == "LINE REACTOR")
                    {
                        // attach element is LINE REACTOR
                        entityId = (await _context.LineReactors.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).LineReactorId;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                return(entityId);
            }

            // find the sourceEntityId of the Bay
            // sourceEntityType can be AC LINE CIRCUIT,TCSC,BUS REACTOR,TRANSFORMER,Filter Bank,BUS,FSC,LINE REACTOR
            string sourceEntityType      = bayForeign.SourceEntityType;
            int    sourceElementWebUatId = bayForeign.SourceEntityWebUatId;
            int    sourceEntityId        = await getElementId(sourceElementWebUatId, sourceEntityType);

            if (sourceEntityId == -1)
            {
                // encountered an unknown source element type, ideally this should not happen
                _log.LogCritical($"Encountered an unknown source element type {sourceEntityType} while inserting Bay with webUatId {bayForeign.WebUatId} and name {bayForeign.Name}");
                return(null);
            }

            // find the destEntityId of the Bay
            // sourceEntityType can be AC LINE CIRCUIT,TCSC,BUS REACTOR,TRANSFORMER,Filter Bank,BUS,FSC,LINE REACTOR
            string destEntityType = bayForeign.DestEntityType;
            int    destEntityId   = -1;

            if (destEntityType != null)
            {
                int destElementWebUatId = bayForeign.DestEntityWebUatId;
                destEntityId = await getElementId(destElementWebUatId, destEntityType);

                if (destEntityId == -1)
                {
                    // encountered an unknown destination element type, ideally this should not happen
                    _log.LogCritical($"Encountered an unknown destination element type {destEntityType} while inserting Bay with webUatId {bayForeign.WebUatId} and name {bayForeign.Name}");
                    return(null);
                }
            }

            // check if we have to replace the entity completely
            if (opt == EntityWriteOption.Replace && existingBay != null)
            {
                _context.Bays.Remove(existingBay);
            }

            // if entity is not present, then insert or check if we have to replace the entity completely
            if (existingBay == null || (opt == EntityWriteOption.Replace && existingBay != null))
            {
                Bay newBay = new Bay();
                newBay.Name             = bayForeign.Name;
                newBay.BayNumber        = bayForeign.BayNumber;
                newBay.SourceEntityId   = sourceEntityId;
                newBay.SourceEntityType = sourceEntityType;
                newBay.SourceEntityName = bayForeign.SourceEntityName;
                if (destEntityType != null)
                {
                    newBay.DestEntityId   = destEntityId;
                    newBay.DestEntityType = destEntityType;
                    newBay.DestEntityName = bayForeign.DestEntityName;
                }
                newBay.BayTypeId    = bayType.BayTypeId;
                newBay.VoltLevelId  = voltLevel.VoltLevelId;
                newBay.SubstationId = substation.SubstationId;
                newBay.WebUatId     = bayForeign.WebUatId;
                _context.Bays.Add(newBay);
                await _context.SaveChangesAsync();

                return(newBay);
            }

            // check if we have to modify the entity
            if (opt == EntityWriteOption.Modify && existingBay != null)
            {
                existingBay.Name             = bayForeign.Name;
                existingBay.BayNumber        = bayForeign.BayNumber.ToString();
                existingBay.SourceEntityId   = sourceEntityId;
                existingBay.SourceEntityType = sourceEntityType;
                existingBay.SourceEntityName = bayForeign.SourceEntityName;
                if (destEntityType != null)
                {
                    existingBay.DestEntityId   = destEntityId;
                    existingBay.DestEntityType = destEntityType;
                    existingBay.DestEntityName = bayForeign.DestEntityName;
                }
                existingBay.BayTypeId    = bayType.BayTypeId;
                existingBay.VoltLevelId  = voltLevel.VoltLevelId;
                existingBay.SubstationId = substation.SubstationId;
                await _context.SaveChangesAsync();

                return(existingBay);
            }
            return(null);
        }