internal EntityDefinition(Type type) { DefinitionType = type; EntityName = DefinitionType.GetField("EntityName").GetValue(null) as string; EntityCollectionName = DefinitionType.GetField("EntityCollectionName").GetValue(null) as string; foreach (var field in DefinitionType.GetNestedType("ManyToOneRelationships")?.GetFields() ?? Enumerable.Empty <FieldInfo>()) { var relationshipName = field.GetValue(null) as string; if (string.IsNullOrEmpty(relationshipName)) { continue; } var at = field.GetCustomAttribute <RelationshipAttribute>(); _manyToOneRelationships.Add(relationshipName, new Relationship { PrimaryEntityRole = at.Role, NavigationPropertyName = at.NavigationPropertyName, SchemaName = relationshipName, LookupFieldName = at.LookupFieldName, TargetEntityName = at.TargetEntityName }); } foreach (var field in DefinitionType.GetNestedType("OneToManyRelationships")?.GetFields() ?? Enumerable.Empty <FieldInfo>()) { var relationshipName = field.GetValue(null) as string; if (string.IsNullOrEmpty(relationshipName)) { continue; } var at = field.GetCustomAttribute <RelationshipAttribute>(); _oneToManyRelationships.Add(relationshipName, new Relationship { PrimaryEntityRole = at.Role, NavigationPropertyName = at.NavigationPropertyName, SchemaName = relationshipName, LookupFieldName = at.LookupFieldName, TargetEntityName = at.TargetEntityName }); } foreach (var field in DefinitionType.GetNestedType("ManyToManyRelationships")?.GetFields() ?? Enumerable.Empty <FieldInfo>()) { var relationshipName = field.GetValue(null) as string; if (string.IsNullOrEmpty(relationshipName)) { continue; } var at = field.GetCustomAttribute <RelationshipAttribute>(); _manyToManyRelationships.Add(relationshipName, new Relationship { NavigationPropertyName = at.NavigationPropertyName, SchemaName = relationshipName, LookupFieldName = at.LookupFieldName, TargetEntityName = at.TargetEntityName }); } foreach (var field in DefinitionType.GetNestedType("Columns")?.GetFields() ?? Enumerable.Empty <FieldInfo>()) { var fieldName = field.GetValue(null) as string; if (string.IsNullOrEmpty(fieldName)) { continue; } _attributeNames.Add(fieldName); _attributeTypes.Add(fieldName, field.GetCustomAttribute <AttributeMetadataAttribute>()?.Type ?? AttributeTypeCode.String); if (_attributeTypes[fieldName] == AttributeTypeCode.DateTime) { _dateTimeBehaviours.Add(fieldName, field.GetCustomAttribute <DateTimeBehaviorAttribute>()?.Behavior ?? DateTimeBehavior.UserLocal); } var keyAttributes = field.GetCustomAttributes <KeyAttribute>().Select(k => k.KeyName).ToList(); if (keyAttributes.Any()) { _keyInformations.Add(fieldName, keyAttributes); } var lookupAttributes = field.GetCustomAttributes <CrmLookupAttribute>().ToList(); if (lookupAttributes.Any()) { _crmLookupAttributes.Add(fieldName, lookupAttributes); } var primaryAttribute = field.GetCustomAttribute <PrimaryAttributeAttribute>(); if (primaryAttribute != null) { switch (primaryAttribute.Type) { case PrimaryAttributeType.Id: PrimaryIdAttributeName = fieldName; break; case PrimaryAttributeType.Name: PrimaryNameAttributeName = fieldName; break; case PrimaryAttributeType.Image: PrimaryImageAttributeName = fieldName; break; default: throw new ArgumentOutOfRangeException(); } } } }