コード例 #1
0
 internal static void SeedData(ITAMDbContext context)
 {
     context.Database.CreateIfNotExists();
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: KeesHiemstra/QuickTests
        private static void ImportJson()
        {
            Console.WriteLine(Inventory.ComputerName);

            using (ITAMDbContext db = new ITAMDbContext(DbConnection))
            {
                using (Win32_Product_SQL product_SQL = new Win32_Product_SQL())
                {
                    foreach (var productItem in Inventory.win32_Product.Items)
                    {
                        // Clear product_SQL, based on Inventory.win32_Product.Items
                        foreach (var property in productItem.GetType().GetProperties())
                        {
                            property.SetValue(product_SQL, null);
                        }
                        product_SQL.ComputerName = Inventory.ComputerName;

                        // Transfer data if not <null>
                        foreach (var property in productItem.GetType().GetProperties())
                        {
                            string value = (string)property.GetValue(productItem);
                            if (value != "<null>")
                            {
                                property.SetValue(product_SQL, value);
                            }
                        }

                        try
                        {
                            db.Product.Add(product_SQL);
                            db.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            string message = $"{ex.Source}\n{ex.Message}\n\n";
                            foreach (var item in productItem.GetType().GetProperties())
                            {
                                string value = (string)item.GetValue(productItem);
                                message += $"{item.Name.PadRight(17)} = {value.Length.ToString().PadLeft(3)}: {value}\n";
                            }
                            MessageBox.Show(message, $"Error {Inventory.ComputerName}: 0x{ex.HResult:X}", MessageBoxButton.OK, MessageBoxImage.Error);
                            //Console.WriteLine($"==== {product_SQL.ComputerName}");
                            //Console.WriteLine($"Name         = {productItem.Name}: {productItem.Name.Length}");
                            //Console.WriteLine($"Vendor       = {productItem.Vendor}: {productItem.Vendor.Length}");
                            //Console.WriteLine($"Version      = {productItem.Version}: {productItem.Version.Length}");
                            //Console.WriteLine($"IdentifyNr   = {productItem.IdentifyingNumber}: {productItem.IdentifyingNumber.Length}");
                            //Console.WriteLine($"InstalDate   = {productItem.InstallDate}: {productItem.InstallDate.Length}");
                            //Console.WriteLine($"InstalLocati = {productItem.InstallLocation}: {productItem.InstallLocation.Length}");
                            //Console.WriteLine($"InstallSoure = {productItem.InstallSource}: {productItem.InstallSource.Length}");
                            //Console.WriteLine($"LocalPackage = {productItem.LocalPackage}: {productItem.LocalPackage.Length}");
                            //Console.WriteLine($"PackageCache = {productItem.PackageCache}: {productItem.PackageCache.Length}");
                            //Console.WriteLine($"PackageCode  = {productItem.PackageCode}: {productItem.PackageCode.Length}");
                            //Console.WriteLine($"PackageName  = {productItem.PackageName}: {productItem.PackageName.Length}");
                            //Console.WriteLine($"HelpLink     = {productItem.HelpLink}: {productItem.HelpLink.Length}");
                            //Console.WriteLine($"URLInfoAbout = {productItem.URLInfoAbout}: {productItem.URLInfoAbout.Length}");
                            //Console.WriteLine($"URLUpdateInf = {productItem.URLUpdateInfo}: {productItem.URLUpdateInfo.Length}");
                        }
                    }
                }
            }
        }