public void CanCheckIfConstraintIsValid() { var customer = new Customer { Number = 61, Firstname = "Peter John", Lastname = "Sylvester", Email = "*****@*****.**" }; var checkResult = _session.CheckForUniqueConstraints(customer); Assert.IsFalse(checkResult.ConstraintsAreFree()); }
static void Main(string[] args) { { Console.WriteLine("RavenDb Test program started."); SimulateErrorOnInserting(); SimulateErrorOnUpdating(); SimulateCheckingUniqueConstraintsBeforeInserting(); Console.WriteLine("Press anykey to exit."); Console.ReadKey(); } void SimulateErrorOnInserting() { Console.WriteLine("Simulate Error On Inserting..."); using (IDocumentSession session = DocumentStoreHolder.Store.OpenSession()) { // insert Product 1 var product1 = new Product { ProductId = new Guid(product1Id), ProductName = "Laptop", Gtin = "ABC12345" }; session.Store(product1, product1.ProductId.ToString()); // insert Product 2 with same Gtin var product2 = new Product { ProductId = new Guid(product2Id), ProductName = "Monitor", Gtin = "ABC12345" }; session.Store(product2, product2.ProductId.ToString()); try { session.SaveChanges(); } catch (ErrorResponseException ex) { if (ex.Message.Contains("OperationVetoedException") && ex.Message.Contains("UniqueConstraintsPutTrigger")) { Console.WriteLine($"Product Gtin is not unique."); } else { Console.WriteLine(ex.Message); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } Console.WriteLine(new string('-', 50)); } void SimulateErrorOnUpdating() { Console.WriteLine("Simulate Error On Updating..."); using (IDocumentSession session = DocumentStoreHolder.Store.OpenSession()) { // insert Product 1 var product1 = new Product { ProductId = new Guid(product1Id), ProductName = "Laptop", Gtin = "ABC12345" }; session.Store(product1, product1.ProductId.ToString()); // insert Product 2 with different Gtin var product2 = new Product { ProductId = new Guid(product2Id), ProductName = "Monitor", Gtin = "XYZ12345" }; session.Store(product2, product2.ProductId.ToString()); // update Product2 Gtin with Product1 Gtin var product = session.Load <Product>(product2Id); product.Gtin = "ABC12345"; try { session.SaveChanges(); } catch (ErrorResponseException ex) { if (ex.Message.Contains("OperationVetoedException") && ex.Message.Contains("UniqueConstraintsPutTrigger")) { Console.WriteLine($"Product Gtin is not unique."); } else { Console.WriteLine(ex.Message); } } } Console.WriteLine(new string('-', 50)); } void SimulateCheckingUniqueConstraintsBeforeInserting() { Console.WriteLine("Simulate Checking Unique Constraints Before Inserting..."); using (IDocumentSession session = DocumentStoreHolder.Store.OpenSession()) { // insert Product 1 var product1 = new Product { ProductId = new Guid(product1Id), ProductName = "Laptop", Gtin = "ABC12345" }; var checkResult = session.CheckForUniqueConstraints(product1); // insert only if there are no unique constraints if (checkResult.ConstraintsAreFree()) { session.Store(product1, product1.ProductId.ToString()); session.SaveChanges(); } } using (IDocumentSession session = DocumentStoreHolder.Store.OpenSession()) { // insert Product 2 with same Gtin var product2 = new Product { ProductId = new Guid(product2Id), ProductName = "Monitor", Gtin = "ABC12345" }; var checkResult = session.CheckForUniqueConstraints(product2); // insert only if there are no unique constraints if (checkResult.ConstraintsAreFree()) { session.Store(product2); } else { var existingProduct = checkResult.DocumentForProperty(x => x.Gtin); Console.WriteLine($"Gtin value: {product2.Gtin} belongs to product id: {existingProduct.ProductId}"); } } Console.WriteLine(new string('-', 50)); } }