private static IEnumerable <IProperty> FindPrincipals( IProperty property, IList <IProperty> visited) { var concreteProperty = property.AsProperty(); if (concreteProperty.ForeignKeys != null) { foreach (var foreignKey in concreteProperty.ForeignKeys) { for (var propertyIndex = 0; propertyIndex < foreignKey.Properties.Count; propertyIndex++) { if (property == foreignKey.Properties[propertyIndex]) { var principal = foreignKey.PrincipalKey.Properties[propertyIndex]; if (!visited.Contains(principal)) { yield return(principal); visited.Add(principal); foreach (var found in FindPrincipals(principal, visited)) { yield return(found); } } } } } } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> protected override Expression VisitMethodCall(MethodCallExpression methodCallExpression) { Expression newExpression = null; if (methodCallExpression.Method.IsEFPropertyMethod()) { var newArguments = VisitAndConvert( new List <Expression> { methodCallExpression.Arguments[0], methodCallExpression.Arguments[1] }.AsReadOnly(), nameof(VisitMethodCall)); Expression targetExpression = newArguments[0]; IEntityType entityType = _model .FindEntityType(targetExpression.Type); IProperty property = entityType .FindProperty((string)((ConstantExpression)newArguments[1]).Value); PropertyInfo propertyInfo = property.PropertyInfo; if (property.IsShadowProperty && property.IsForeignKey()) { IForeignKey foreignKey = property.AsProperty().ForeignKeys.Single(); INavigation navigation = foreignKey.PrincipalEntityType == entityType || foreignKey.IsSelfPrimaryKeyReferencing() ? foreignKey.PrincipalToDependent : foreignKey.DependentToPrincipal; if (navigation != null) { targetExpression = Expression.MakeMemberAccess(targetExpression, navigation.PropertyInfo); IEntityType targetEntityType = navigation.GetTargetType(); property = targetEntityType.FindPrimaryKey().Properties.Single(); propertyInfo = property.PropertyInfo; } } if (propertyInfo.PropertyType != typeof(string)) { newExpression = Expression.Convert( Expression.MakeMemberAccess(targetExpression, propertyInfo), typeof(Nullable <>).MakeGenericType(propertyInfo.PropertyType)); } } return(newExpression ?? base.VisitMethodCall(methodCallExpression)); }
/// <summary> /// /// </summary> /// <param name="p"></param> /// <param name="value"></param> /// <returns></returns> private static object ReadFieldValue(IProperty p, object _value) { object value = null; if (p.IsForeignKey()) { var fk = p.AsProperty().ForeignKeys.FirstOrDefault(); var entvalue = fk.DependentToPrincipal.PropertyInfo.GetValue(_value); value = fk.PrincipalKey.DeclaringEntityType.GetProperty(p.Name).PropertyInfo.GetValue(entvalue); } else { value = p.PropertyInfo?.GetValue(_value); } return(value); }
private void GenerateKeyAttribute(IProperty property) { var key = property.AsProperty().PrimaryKey; if (key?.Properties.Count == 1) { if (key is Key concreteKey && key.Properties.SequenceEqual(new KeyDiscoveryConvention(null).DiscoverKeyProperties(concreteKey.DeclaringEntityType, concreteKey.DeclaringEntityType.GetProperties().ToList()))) { return; } if (key.Relational().Name != ConstraintNamer.GetDefaultName(key)) { return; } _sb.AppendLine(new AttributeWriter(nameof(KeyAttribute))); } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> // Issue#11266 This method is being used by provider code. Do not break. public static IProperty FindPrincipal([NotNull] this IProperty property) { var concreteProperty = property.AsProperty(); if (concreteProperty.ForeignKeys != null) { foreach (var foreignKey in concreteProperty.ForeignKeys) { for (var propertyIndex = 0; propertyIndex < foreignKey.Properties.Count; propertyIndex++) { if (property == foreignKey.Properties[propertyIndex]) { return(foreignKey.PrincipalKey.Properties[propertyIndex]); } } } } return(null); }
private void GenerateKeyAttribute(IProperty property) { var key = property.AsProperty().PrimaryKey; if (key?.Properties.Count == 1) { if (key is Key concreteKey && key.Properties.SequenceEqual(new KeyDiscoveryConvention(null).DiscoverKeyProperties(concreteKey.DeclaringEntityType, concreteKey.DeclaringEntityType.GetProperties().ToList()))) { return; } if (key.Relational().Name != ConstraintNamer.GetDefaultName(key)) { return; } PropertyAnnotationsData.Add(new Dictionary <string, object> { { "property-annotation", new AttributeWriter(nameof(KeyAttribute)) }, }); } }
private static void AddPrincipals(IProperty property, List <IProperty> visited) { var concreteProperty = property.AsProperty(); if (concreteProperty.ForeignKeys != null) { foreach (var foreignKey in concreteProperty.ForeignKeys) { for (var propertyIndex = 0; propertyIndex < foreignKey.Properties.Count; propertyIndex++) { if (property == foreignKey.Properties[propertyIndex]) { var principal = foreignKey.PrincipalKey.Properties[propertyIndex]; if (!visited.Contains(principal)) { visited.Add(principal); AddPrincipals(principal, visited); } } } } } }