public void CanAddDevice()
        {
            using (EF_ZMT_DbContext.EF_ZMT_DbContext context = new EF_ZMT_DbContext.EF_ZMT_DbContext())
            {
                context.Context.ExecuteStoreCommand("DELETE FROM Device");
                context.Context.ExecuteStoreCommand("DELETE FROM PeripheralDevice");
                context.Context.ExecuteStoreCommand("DELETE FROM FixedAsset");
                Assert.AreEqual(context.Context.Devices.Count(), 0);
                Assert.AreEqual(context.Context.PeripheralDevices.Count(), 0);
                Assert.AreEqual(context.Context.FixedAssets.Count(), 0);
                FixedAssetService transaction = new FixedAssetService();

                FixedAsset asset = new FixedAsset()
                {
                    inventory_number = "222222",
                    cassation = false,
                    date_of_activation = DateTime.Now
                };
                PeripheralDevice pd = new PeripheralDevice() { name = "Kamera" };  
                Device device = new Device() { model = "x400", producer = "Sony", PeripheralDevice = pd, FixedAsset = asset };
                transaction.AddDevice(device);
                Assert.AreEqual(context.Context.Devices.Count(), 1);
                Assert.AreEqual(context.Context.PeripheralDevices.Count(), 1);
                Assert.AreEqual(context.Context.FixedAssets.Count(), 1);
            }
        }
 public void CantAddPeripheralDevice()
 {
     using(EF_ZMT_DbContext.EF_ZMT_DbContext context = new EF_ZMT_DbContext.EF_ZMT_DbContext())
     {
         FixedAssetService transaction = new FixedAssetService();
         context.Context.ExecuteStoreCommand("DELETE FROM PeripheralDevice");
         PeripheralDevice device = new PeripheralDevice() { name = "printer" };
         transaction.AddPeripheralDevice(device);
         Assert.AreEqual(context.Context.PeripheralDevices.Count(), 1);
         device = context.Context.PeripheralDevices.FirstOrDefault(x => x.name == "printer");
         Assert.IsNotNull(device);
     }
 }
        public void CanEditDevice()
        {
            using (EF_ZMT_DbContext.EF_ZMT_DbContext context = new EF_ZMT_DbContext.EF_ZMT_DbContext())
            {
                context.Context.ExecuteStoreCommand("DELETE FROM Device");
                context.Context.ExecuteStoreCommand("DELETE FROM PeripheralDevice");
                context.Context.ExecuteStoreCommand("DELETE FROM FixedAsset");
                Assert.AreEqual(context.Context.Devices.Count(), 0);
                Assert.AreEqual(context.Context.PeripheralDevices.Count(), 0);
                Assert.AreEqual(context.Context.FixedAssets.Count(), 0);
                FixedAssetService transaction = new FixedAssetService();

                FixedAsset asset = new FixedAsset()
                {
                    inventory_number = "222222",
                    cassation = false,
                    date_of_activation = DateTime.Now
                };
                PeripheralDevice pd = new PeripheralDevice() { name = "Kamera" };
                Device device = new Device() { model = "x400", producer = "Sony", PeripheralDevice = pd, FixedAsset = asset };
                transaction.AddDevice(device);
                Assert.AreEqual(context.Context.Devices.Count(), 1);
                Assert.AreEqual(context.Context.PeripheralDevices.Count(), 1);
                Assert.AreEqual(context.Context.FixedAssets.Count(), 1);

                pd = new PeripheralDevice() { name = "Aparat" };
                context.Context.PeripheralDevices.AddObject(pd);
                context.SaveChanges();
                Assert.AreEqual(context.Context.PeripheralDevices.Count(), 2);
                device = context.Context.Devices.FirstOrDefault(x => x.model == "x400");
                Assert.IsNotNull(device);
                device.PeripheralDevice = pd;
                device.model = "x500";
                transaction.EditDevice(device);

                device = context.Context.Devices.FirstOrDefault(x => x.model == "x500");
                Assert.IsNotNull(device);
                Assert.AreEqual(device.model, "x500");
                Assert.AreEqual(device.PeripheralDevice.name, "Aparat");
                Assert.AreEqual(context.Context.Devices.Count(), 1);
            }
        }
 public void AddPeripheralDevice(PeripheralDevice peripheralDevice)
 {
     using (EF_ZMT_DbContext.EF_ZMT_DbContext context = new EF_ZMT_DbContext.EF_ZMT_DbContext())
     {
         try
         {
             context.Context.PeripheralDevices.AddObject(peripheralDevice);
             context.SaveChanges();
         }
         catch (UpdateException)
         {
             throw new FaultException(string.Format(
             "Nie udało się dodać urządzenia peryferyjnego. Popraw dane i spróbuj ponownie"));
         }
         catch (Exception ex)
         {
             throw new Exception(ex.Message.ToString());
         }
     }
 }
 public void EditPeripheralDevice(PeripheralDevice peripheralDevice)
 {
     using (EF_ZMT_DbContext.EF_ZMT_DbContext context = new EF_ZMT_DbContext.EF_ZMT_DbContext())
     {
         try
         {
             PeripheralDevice originalPeripheralDevice = context.Context.PeripheralDevices.FirstOrDefault(p => p.id == peripheralDevice.id);
             context.Context.PeripheralDevices.ApplyCurrentValues(peripheralDevice);
             context.SaveChanges();
         }
         catch (UpdateException)
         {
             throw new FaultException(string.Format(
             "Nie udało się edytować urządzenia peryferyjnego. Popraw dane i spróbuj ponownie"));
         }
         catch (Exception ex)
         {
             throw new Exception(ex.Message.ToString());
         }
     }
 }
 public void DeletePeripheralDevice(PeripheralDevice peripheralDevice)
 {
     using (EF_ZMT_DbContext.EF_ZMT_DbContext context = new EF_ZMT_DbContext.EF_ZMT_DbContext())
     {
         try
         {
             context.Context.PeripheralDevices.Attach(peripheralDevice);
             context.Context.PeripheralDevices.DeleteObject(peripheralDevice);
             context.SaveChanges();
         }
         catch (UpdateException)
         {
             throw new FaultException(string.Format(
             "Nie udało się usunąć urządzenia peryferyjnego. Prawdopodobnie nie istnieje"));
         }
         catch (Exception ex)
         {
             throw new Exception(ex.Message.ToString());
         }
     }
 }
     private void FixupPeripheralDevice(PeripheralDevice previousValue)
     {
         if (IsDeserializing)
         {
             return;
         }
 
         if (previousValue != null && previousValue.Devices.Contains(this))
         {
             previousValue.Devices.Remove(this);
         }
 
         if (PeripheralDevice != null)
         {
             if (!PeripheralDevice.Devices.Contains(this))
             {
                 PeripheralDevice.Devices.Add(this);
             }
 
             id_peripheral_device = PeripheralDevice.id;
         }
         if (ChangeTracker.ChangeTrackingEnabled)
         {
             if (ChangeTracker.OriginalValues.ContainsKey("PeripheralDevice")
                 && (ChangeTracker.OriginalValues["PeripheralDevice"] == PeripheralDevice))
             {
                 ChangeTracker.OriginalValues.Remove("PeripheralDevice");
             }
             else
             {
                 ChangeTracker.RecordOriginalValue("PeripheralDevice", previousValue);
             }
             if (PeripheralDevice != null && !PeripheralDevice.ChangeTracker.ChangeTrackingEnabled)
             {
                 PeripheralDevice.StartTracking();
             }
         }
     }