public static void ApplicationInternalErrorLog(string errorMessage)
        {
            //Obtaining the data source
            LoggingContext dbEventLog = new LoggingContext();

            int nextId = dbEventLog.InternalErrors.Count() + 1;
            // InternalErrorDBItem ie = new InternalErrorDBItem() { Id = nextId, LogTime = System.DateTime.Now, EventTime = System.DateTime.Now, ErrorMessage = errorMessage };
            InternalErrorDBItem ie = new InternalErrorDBItem()
            {
                LogTime = System.DateTime.Now, EventTime = System.DateTime.Now, ErrorMessage = errorMessage
            };

            dbEventLog.InternalErrors.Add(ie);

            dbEventLog.SaveChanges();
        }
        public static void FillUnitsTable()
        {
            //Obtaining the data source
            LoggingContext dbEventLog = new LoggingContext();

            int OtherDerivedUnitClassSize = OtherDerivedUnitStrings.Count;

            //int DeleteClassSize = BaseUnitsClassSize + NamedDerivedUnitsClassSize + OtherDerivedUnitClassSize;
            int DeleteClassSize = OtherDerivedUnitBaseNumber + OtherDerivedUnitClassSize;

            // Remove all units from dbEventLog.Units
            //Create compiled query
            var fnUnitsOfClass = CompiledQuery.Compile((MeasurementsDataContext dbEventLog1, int UnitClass, int ClassSize) =>
                                                       from c in dbEventLog1.Units
                                                       where c.Id >= UnitClass
                                                       where c.Id < (UnitClass + ClassSize)
                                                       select c);

            using (IDbContextTransaction ts = dbEventLog.Database.BeginTransaction())
            {
                try
                {
                    // Execute the query
                    // Remove all units from dbEventLog.Units
                    dbEventLog.Units.RemoveRange(dbEventLog.Units);

                    dbEventLog.SaveChanges();


                    // SET IDENTITY_INSERT Units ON  to try to avoid exception in final dbEventLog.SaveChanges(); Entity framwork core SqlException: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF.
                    //dbEventLog.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Units ON");

                    // Fill base units into dbEventLog.Units
                    foreach (PhysicalMeasure.BaseUnit pu in PhysicalMeasure.Physics.SI_Units.BaseUnits)
                    {
                        UnitDBItem u = new UnitDBItem()
                        {
                            Id = BaseUnitBaseNumber + pu.BaseUnitNumber, Name = pu.Name, Symbol = pu.Symbol, Exponents = pu.Exponents
                        };
                        dbEventLog.Units.Add(u);
                    }


                    // Fill named derived units into dbEventLog.Units
                    int NamedDerivedUnitIndex = 0;
                    foreach (PhysicalMeasure.NamedDerivedUnit pu in PhysicalMeasure.Physics.SI_Units.NamedDerivedUnits)
                    {
                        UnitDBItem u = new UnitDBItem()
                        {
                            Id = NamedDerivedUnitBaseNumber + NamedDerivedUnitIndex, Name = pu.Name, Symbol = pu.Symbol, Exponents = pu.Exponents
                        };
                        dbEventLog.Units.Add(u);

                        NamedDerivedUnitIndex++;
                    }


                    // Fill Convertible units into dbEventLog.Units
                    int NamedConvertibleUnitIndex = 0;
                    foreach (PhysicalMeasure.ConvertibleUnit pu in PhysicalMeasure.Physics.SI_Units.ConvertibleUnits)
                    {
                        UnitDBItem u = new UnitDBItem()
                        {
                            Id = NamedConvertibleUnitBaseNumber + NamedConvertibleUnitIndex, Name = pu.Name, Symbol = pu.Symbol, Exponents = pu.Exponents, ConversionFactor = pu.Conversion.LinearScale, ConversionOffset = pu.Conversion.LinearOffset
                        };
                        dbEventLog.Units.Add(u);

                        NamedConvertibleUnitIndex++;
                    }


                    // Fill named derived units into dbEventLog.Units
                    int OtherDerivedUnitIndex = 0;
                    foreach (string unitStr in OtherDerivedUnitStrings)
                    {
                        PhysicalUnit     pu = PhysicalUnit.Parse(unitStr);
                        PhysicalQuantity pq = pu.ConvertToSystemUnit().ConvertToDerivedUnit();

                        UnitDBItem u = new UnitDBItem()
                        {
                            Id = OtherDerivedUnitBaseNumber + OtherDerivedUnitIndex, Name = pu.ToPrintString(), Exponents = pq.Unit.Exponents, ConversionFactor = 1 / pq.Value
                        };
                        dbEventLog.Units.Add(u);

                        OtherDerivedUnitIndex++;
                    }

                    // Entity framwork core SqlException: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF.
                    // dbEventLog.Database.GetDbConnection();
                    //dbEventLog.ChangeTracker.Context.
                    // dbEventLog.ChangeTracker.QueryTrackingBehavior
                    // dbEventLog.
                    dbEventLog.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Units ON");
                    dbEventLog.SaveChanges();

                    ts.Commit();
                }
                catch (Exception ex)
                {
                    InternalErrorDBItem.ApplicationInternalErrorLog("Error when FillUnitsTable. " + ex.Message);
                    ts.Rollback();
                    // throw;
                }
            }
        }
        public static void FillUnitListsTable()
        {
            //Obtaining the data source
            LoggingContext dbEventLog = new LoggingContext();


            //Create compiled query

            /*
             * var fnUnitIdFromName = CompiledQuery.Compile((MeasurementsDataContext dbEventLog1, string UnitName) =>
             *  from u in dbEventLog.Units
             *  where u.Name == UnitName
             *  select u.Id);
             */
            List <int> fnUnitIdFromName(LoggingContext dbEventLog1, string UnitName)
            {
                var temp = from u in dbEventLog.Units
                           where u.Name == UnitName
                           select u.Id;

                return(temp.ToList());
            }

            // Execute the query
            // Remove all unit lists from dbEventLog.UnitLists
            dbEventLog.UnitListElements.RemoveRange(dbEventLog.UnitListElements);

            dbEventLog.SaveChanges();

            // Fill named derived units into dbEventLog.Units
            int UnitListId = UnitListBaseNumber;

            int UnitListElementIndex = 0;

            foreach (string unitStr in UnitList.FavoritUnitStrings)
            {
                // IPhysicalUnit pu = PhysicalMeasure.PhysicalUnit.Parse(unitStr);
                // IPhysicalQuantity pq = pu.ConvertToSystemUnit();

                var q = fnUnitIdFromName(dbEventLog, unitStr);

                int UnitListElementUnitId = q.ElementAtOrDefault(0);
                // Debug.Assert(UnitListElementUnitId != 0);

                if (UnitListElementUnitId != 0)
                {
                    UnitListElementDBItem ul_element = new UnitListElementDBItem()
                    {
                        UnitId = UnitListElementUnitId, ListId = UnitListId, ElementIndex = UnitListElementIndex + 1
                    };
                    dbEventLog.UnitListElements.Add(ul_element);
                }
                else
                {
                    InternalErrorDBItem.ApplicationInternalErrorLog("Error when adding '" + unitStr + "' as favorite unit. Found no unit named '" + unitStr + "'");
                }

                UnitListElementIndex++;
            }

            dbEventLog.SaveChanges();
        }
 public ErrorItemTreeNode(ErrorItemViewKind viewKind, InternalErrorDBItem e, ListViewTreeView LogItemTreeView = null)
     : base(e.GetErrorItemText(viewKind & (ErrorItemViewKind.DBId | ErrorItemViewKind.LogTime | ErrorItemViewKind.EventTime | ErrorItemViewKind.ErrorMessage)), ListViewClassIconIndexes.II_InternalError, LogItemTreeView)
 {
 }