public void ShouldAllowSettingServerGeneratedPropertiesWithIdentityInsert() { using (var trans = _context.Database.BeginTransaction()) { var product = TestHelpers.CreateProduct("1"); var g = Guid.NewGuid(); product.Rowguid = g; product.ProductId = 99999; _context.Product.Add(product); Assert.Equal(g, product.Rowguid); var sql = $"SET IDENTITY_INSERT " + $"{_context.GetSqlServerTableName<Product>()} ON"; _context.Database.ExecuteSqlCommand(sql); _context.SaveChanges(); sql = $"SET IDENTITY_INSERT " + $"{_context.GetSqlServerTableName<Product>()} OFF"; _context.Database.ExecuteSqlCommand(sql); Assert.Equal(g, product.Rowguid); Assert.Equal(99999, product.ProductId); trans.Rollback(); } }
public void ShouldThrowErrorWithConcurrencyViolation() { using (var transaction = _context.Database.BeginTransaction()) { var product = TestHelpers.CreateProduct("1"); _context.Product.Add(product); _context.SaveChanges(); string sql = $"UPDATE {_context.GetSqlServerTableName<Product>()} " + $"SET Name = 'Foo' WHERE {nameof(Product.ProductId)} = {product.ProductId}"; _context.Database.ExecuteSqlCommand(sql); product.MakeFlag = false; try { _context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { //Get Current value for name var entityInError = ex.Entries[0]; PropertyValues originalValues = entityInError.OriginalValues; PropertyValues proposedValues = entityInError.CurrentValues; //This makes a database call to get the current values in the database PropertyValues databaseValues = entityInError.GetDatabaseValues(); Debug.WriteLine("Name"); Debug.WriteLine($"Original Name: {originalValues.GetValue<string>(nameof(Product.Name))}"); Debug.WriteLine($"Proposed Name: {proposedValues.GetValue<string>(nameof(Product.Name))}"); Debug.WriteLine($"Database Name: {databaseValues.GetValue<string>(nameof(Product.Name))}"); Debug.WriteLine("Make Flag"); //this is just a different syntax to above Debug.WriteLine( $"Original Make Flag: {entityInError.Property(nameof(Product.MakeFlag)).OriginalValue}"); Debug.WriteLine( $"Proposed Make Flag: {entityInError.Property(nameof(Product.MakeFlag)).CurrentValue}"); var modifiedProperties = entityInError.Properties.Where(p => p.IsModified).ToList(); foreach (var p in modifiedProperties) { Debug.WriteLine($"{p.Metadata.Name}:{p.OriginalValue}:{p.CurrentValue}"); } //This reloads the entity from the database, discarding any proposed changes //entityInError.Reload(); } transaction.Rollback(); } }
public void ShouldGetSqlServerSchemaAndTableName() { var schemaTableName = _context.GetSqlServerTableName <Product>(); Assert.Equal("Production.Product", schemaTableName); }