public void RelatedEntityAttribute_Merge_TwoAttribs_LeftPopulated_Test() { // Arrange var re1 = new RelatedEntityAttribute("Entity2") { EntityAlias = "Alias1", AutoExpand = true, Filter = "TypId eq 1", ForeignKeyProperty = "Name", ForeignKeyType = typeof(string), Property = "Entity2Name", GetAll = false, Nullable = true, RelatedEntityAlias = "E2", RelatedEntityMustExist = false, Entity = "Entity1" }; var re2 = new RelatedEntityAttribute("Entity2") { Entity = "Entity1", Property = "Entity2Name" }; // Act var result = RelatedEntityAttributeExtensions.Merge(re1, re2); // Assert Assert.AreEqual("Alias1", result.EntityAlias, "RelatedEntityAlias"); Assert.IsTrue(result.AutoExpand, "AutoExpand"); Assert.AreEqual("TypId eq 1", result.Filter, "Filter"); Assert.AreEqual("Name", result.ForeignKeyProperty, "ForeignKeyProperty"); Assert.AreEqual(typeof(string), result.ForeignKeyType, "ForeignKeyType"); Assert.AreEqual("Entity2Name", result.Property, "Property"); Assert.IsFalse(result.GetAll, "GetAll"); Assert.IsTrue(result.Nullable, "Nullable"); Assert.AreEqual("E2", result.RelatedEntityAlias, "RelatedEntityAlias"); Assert.IsFalse(result.RelatedEntityMustExist, "RelatedEntityMustExist"); Assert.AreEqual("Entity1", result.Entity, "Entity"); }
public void GetAttributesToExpandUserTypeNoAutoexpandTest() { // Arrange List <string> entitiesToExpand = new List <string> { "UserType" }; var evaluator = new AttributeEvaluator(); var expected = new RelatedEntityAttribute("UserType") { Property = "UserTypeId" }; // Act var actual = evaluator.GetAttributesToExpand <RelatedEntityAttribute>(typeof(User), entitiesToExpand).ToList(); // Assert Assert.AreEqual(1, actual.Count); foreach (var prop in typeof(RelatedEntityAttribute).GetProperties()) { Assert.AreEqual(prop.GetValue(expected), prop.GetValue(actual[0])); } }
public void RelatedEntityNavigationPropertyBuilder_Build_ValidAttribute_AllowNonExistantValue_Test() { // Arrange var funcs = new FuncList <string, string>(); var unitUnderTest = new RelatedEntityNavigationPropertyBuilder(funcs); var relatedEntityAttribute = new RelatedEntityAttribute("Entity2") { AllowedNonExistentValue = 0 }; // Act var result = unitUnderTest.Build(relatedEntityAttribute); // Assert Assert.IsTrue(result is CsdlNavigationProperty); Assert.AreEqual("self.Entity2", result.Type); Assert.AreEqual(CsdlConstants.NavigationProperty, result.Kind); var defaultValue = result.CustomData[CsdlConstants.Default] as CsdlNameValue; Assert.AreEqual(relatedEntityAttribute.AllowedNonExistentValue, defaultValue.Value); Assert.AreEqual(relatedEntityAttribute.AllowedNonExistentValueName, defaultValue.Name); Assert.IsFalse(result.IsCollection); Assert.IsFalse(result.Nullable); }
public static CsdlNavigationProperty ToNavigationProperty(this RelatedEntityAttribute relatedEntityAttribute, string schemaOrAlias = CsdlConstants.DefaultSchemaOrAlias, CustomPropertyDataFuncs CustomPropertyDataFuncs = null) { CustomPropertyDataFuncs = CustomPropertyDataFuncs ?? new CustomPropertyDataFuncs(); return(new RelatedEntityNavigationPropertyBuilder(CustomPropertyDataFuncs).Build(relatedEntityAttribute, schemaOrAlias)); }
private static string ConstructCopySql(Type entity, TableAttribute tableAttribute, RelatedEntityAttribute relatedAttribute, int keyValue) { FieldNamesCache.TryGetValue(entity.FullName ?? throw new InvalidOperationException(), out var fieldNames); if (string.IsNullOrEmpty(fieldNames)) { var fields = entity.GetProperties(); fieldNames = string.Join(", ", fields.Select(x => $"[{x.Name}]").ToArray()); FieldNamesCache.Add(entity.FullName, fieldNames); } var sql = new StringBuilder(); if (!ConstrainDisabledEntities.Contains(entity.FullName)) { sql.AppendLine($"ALTER TABLE [daes_{_dbPrefix}].[{tableAttribute.TableSchema}].[{tableAttribute.TableName}] NOCHECK CONSTRAINT ALL"); ConstrainDisabledEntities.Add(entity.FullName); } if (tableAttribute.HasIdentity) { sql.AppendLine($"SET IDENTITY_INSERT [daes_{_dbPrefix}].[{tableAttribute.TableSchema}].[{tableAttribute.TableName}] ON "); } sql.AppendLine($@"INSERT INTO [daes_{_dbPrefix}].[{tableAttribute.TableSchema}].[{tableAttribute.TableName}] ({fieldNames}) "); sql.AppendLine($@"SELECT {fieldNames} FROM [saes_{_dbPrefix}].[{tableAttribute.TableSchema}].[{tableAttribute.TableName}] src "); sql.AppendLine($@"WHERE {tableAttribute.PrimaryKey} = {keyValue} "); sql.AppendLine($@"AND NOT EXISTS(SELECT 1 FROM [daes_{_dbPrefix}].[{tableAttribute.TableSchema}].[{tableAttribute.TableName}] dst WHERE dst.{tableAttribute.PrimaryKey} = src.{tableAttribute.PrimaryKey})"); if (tableAttribute.HasIdentity) { sql.AppendLine($"SET IDENTITY_INSERT [daes_{_dbPrefix}].[{tableAttribute.TableSchema}].[{tableAttribute.TableName}] OFF "); } return(sql.ToString()); }