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;
                }
            }
        }
예제 #2
0
        //protected UnitKind _kindofUnit = UnitKind.BaseUnit;

        //public UnitKind KindOfUnit { get { return _kindofUnit; } }
        //public override string Text { get { return Name + "  " + (!string.IsNullOrWhiteSpace(Symbol) ? Symbol : ExponentsText); } set { /* Name = value; */ } }

        /*
         * public string GetUnitItemText(UnitItemViewKind UnitViewKind)
         * {
         *  string  str ="";
         *
         *  if ((UnitViewKind & UnitItemViewKind.DBId) != 0)
         *  {
         *      str += "[" + Id.ToString() + "]";
         *  }
         *
         *  if ((UnitViewKind & UnitItemViewKind.Name) != 0)
         *  {
         *      if (!string.IsNullOrWhiteSpace(str))
         *      {
         *          str += " ";
         *      }
         *      str += Name;
         *  }
         *
         *  if ((UnitViewKind & UnitItemViewKind.Symbol) != 0)
         *  {
         *      if (!string.IsNullOrWhiteSpace(str))
         *      {
         *          str += " ";
         *      }
         *      str += Symbol;
         *  }
         *
         *  if (   (((UnitViewKind & UnitItemViewKind.Symbol) != 0) && string.IsNullOrWhiteSpace(Symbol))
         || ((UnitViewKind & UnitItemViewKind.BaseUnits) != 0))
         || {
         ||     if (!string.IsNullOrWhiteSpace(str))
         ||     {
         ||         str += " ";
         ||     }
         ||     str += ExponentsText;
         || }
         ||
         || return str;
         ||}
         ||
         ||
         ||// string.Format();
         ||//public string ExponentsText { get { return PhysicalUnit.MakePhysicalUnit(DimensionExponents.Exponents(Exponents.ToArray()), ConvertionFactor ?? 1.0).ToString(); } set { /*  Name = value; * /  } }
         ||//public string ExponentsText { get { return PhysicalUnit.MakePhysicalUnit(DimensionExponents.Exponents(Exponents.ToArray()), 1.0).ToString(); } set { /*  Name = value; * /  } }
         ||//public string ExponentsText { get { return PhysicalUnit.MakePhysicalUnit(DimensionExponents.Exponents(Exponents.ToArray()), ConversionFactor ?? 1.0, ConversionOffset ?? 0.0).ToString(); } set { /*  Name = value; * /  } }
         ||public string ExponentsText
         ||{
         || get
         || {
         ||     if (!ConversionOffset.HasValue || ConversionOffset.Value == 0)
         ||     {
         ||         return PhysicalUnit.MakePhysicalUnit(Exponents.ToArray().ToSBytes(), ConversionFactor ?? 1.0, ConversionOffset ?? 0.0).ToString();
         ||     }
         ||     else
         ||     {
         ||         return PhysicalUnit.MakePhysicalUnit(Exponents.ToArray().ToSBytes(), ConversionFactor ?? 1.0, ConversionOffset ?? 0.0).ToString();
         ||     }
         || }
         || set { / *  Name = value; * /  }
         ||}
         **/

        /*
         * // Define FillUnitsTable base numbers and class sizes
         * public const int BaseUnitBaseNumber = 1;
         * public const int BaseUnitsClassSize = 16;
         * public const int NamedDerivedUnitBaseNumber = BaseUnitBaseNumber + BaseUnitsClassSize;
         * public const int NamedDerivedUnitsClassSize = 32;
         * public const int NamedConvertibleUnitBaseNumber = NamedDerivedUnitBaseNumber + NamedDerivedUnitsClassSize;
         * public const int NamedConvertibleClassSize = 16;
         * public const int OtherDerivedUnitBaseNumber = NamedConvertibleUnitBaseNumber + NamedConvertibleClassSize;
         *
         * public static List<string> OtherDerivedUnitStrings = new List<string> { "m/s", "Km/h", "Kg/h", "l/h" , "KWh"};
         *
         * public static int OtherDerivedUnitClassSize = OtherDerivedUnitStrings.Count;
         ***/

        public static void FillUnitsTable()
        {
            //Obtaining the data source
            MeasurementsDataContext dbEventLog = new MeasurementsDataContext();

            //List<string> OtherDerivedUnitStrings = new List<string> { "m/s", "Km/h", "Kg/h", "l/h" };

            int OtherDerivedUnitClassSize = OtherDerivedUnitStrings.Count;

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

            // Remove all units from dbEventLog.Units
            //dbEventLog.Units.DeleteAllOnSubmit(dbEventLog.Units);
            //dbEventLog.SubmitChanges();

            //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);

            // Execute the query
            // Remove all units from dbEventLog.Units
            // foreach (Unit u in dbEventLog.Units)
            ////foreach (Unit u in fnUnitsOfClass(dbEventLog, BaseUnitBaseNumber - 1, DeleteClassSize))
            ////{
            ////    dbEventLog.Units.DeleteOnSubmit(u);
            ////}
            // Remove all unit lists from dbEventLog.Units
            dbEventLog.Units.DeleteAllOnSubmit(dbEventLog.Units);


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


            // Fill named derived units into dbEventLog.Units
            int NamedDerivedUnitIndex = 0;

            foreach (PhysicalMeasure.NamedDerivedUnit pu in PhysicalMeasure.Physics.SI_Units.NamedDerivedUnits)
            {
                Unit_LINQ u = new Unit_LINQ()
                {
                    Id = NamedDerivedUnitBaseNumber + NamedDerivedUnitIndex, Name = pu.Name, Symbol = pu.Symbol, Exponents = pu.Exponents
                };
                dbEventLog.Units.InsertOnSubmit(u);

                NamedDerivedUnitIndex++;
            }


            // Fill Convertible units into dbEventLog.Units
            int NamedConvertibleUnitIndex = 0;

            foreach (PhysicalMeasure.ConvertibleUnit pu in PhysicalMeasure.Physics.SI_Units.ConvertibleUnits)
            {
                //Unit u = new Unit() { Id = NamedDerivedUnitBaseNumber + NamedDerivedUnitIndex + NamedConvertibleUnitIndex, Name = pu.Name, Symbol = pu.Symbol, Exponents = pu.UnsignedExponents, ConvertionFactor = 1 / pu.ConvertToPrimaryUnit().Value };
                //Unit u = new Unit() { Id = NamedConvertibleUnitBaseNumber + NamedConvertibleUnitIndex, Name = pu.Name, Symbol = pu.Symbol, Exponents = pu.UnsignedExponents, ConvertionFactor = 1 / pu.ConvertToPrimaryUnit().Value, ConvertionOffset =  };
                Unit_LINQ u = new Unit_LINQ()
                {
                    Id = NamedConvertibleUnitBaseNumber + NamedConvertibleUnitIndex, Name = pu.Name, Symbol = pu.Symbol, Exponents = pu.Exponents, ConversionFactor = pu.Conversion.LinearScale, ConversionOffset = pu.Conversion.LinearOffset
                };
                dbEventLog.Units.InsertOnSubmit(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();

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

                OtherDerivedUnitIndex++;
            }

            dbEventLog.SubmitChanges();
        }