private static void CloneForeignKeys(IReadOnlyEntityType sourceEntityType, IMutableEntityType targetEntityType) { foreach (var foreignKey in sourceEntityType.GetDeclaredForeignKeys()) { var targetPrincipalEntityType = targetEntityType.Model.FindEntityType(foreignKey.PrincipalEntityType.Name); var clonedForeignKey = targetEntityType.AddForeignKey( foreignKey.Properties.Select(p => targetEntityType.FindProperty(p.Name)).ToList(), targetPrincipalEntityType.FindKey( foreignKey.PrincipalKey.Properties.Select(p => targetPrincipalEntityType.FindProperty(p.Name)).ToList()), targetPrincipalEntityType); clonedForeignKey.IsUnique = foreignKey.IsUnique; clonedForeignKey.IsRequired = foreignKey.IsRequired; foreignKey.GetAnnotations().ForEach(annotation => clonedForeignKey[annotation.Name] = annotation.Value); } }
public static IReadOnlyForeignKey?FindDeclaredOwnership([NotNull] this IReadOnlyEntityType entityType) => entityType.GetDeclaredForeignKeys().FirstOrDefault(fk => fk.IsOwnership);