private IAsyncQueryProvider GetQueryProvider()
        {
            if (queryProvider == null)
            {
                queryProvider = currentDbContext.GetDependencies().QueryProvider;
            }

            return(queryProvider);
        }
Ejemplo n.º 2
0
        private IReadOnlyDictionary <IEntityType, ModificationCommandIdentityMapFactory> GetTableSharingIdentityMapFactories(
            IModel model)
        {
            if (_tableSharingIdentityMapFactories != null)
            {
                return(_tableSharingIdentityMapFactories);
            }

            var tables = new Dictionary <(string Schema, string TableName), HashSet <IEntityType> >();

            foreach (var entityType in model.GetEntityTypes())
            {
                var fullName = (entityType.Relational().Schema, entityType.Relational().TableName);
                if (!tables.TryGetValue(fullName, out var mappedEntityTypes))
                {
                    mappedEntityTypes = new HashSet <IEntityType>();
                    tables.Add(fullName, mappedEntityTypes);
                }

                mappedEntityTypes.Add(entityType);
            }

            var sharedTablesMap = new Dictionary <IEntityType, ModificationCommandIdentityMapFactory>();

            foreach (var tableMapping in tables)
            {
                var entityTypes = tableMapping.Value;
                if (entityTypes.Count > 1)
                {
                    var principals = new Dictionary <IEntityType, IReadOnlyList <IEntityType> >(entityTypes.Count);
                    var dependents = new Dictionary <IEntityType, IReadOnlyList <IEntityType> >(entityTypes.Count);
                    foreach (var entityType in entityTypes)
                    {
                        var principalList = new List <IEntityType>();
                        if (!dependents.TryGetValue(entityType, out var dependentList))
                        {
                            dependentList          = new List <IEntityType>();
                            dependents[entityType] = dependentList;
                        }

                        foreach (var foreignKey in entityType.FindForeignKeys(entityType.FindPrimaryKey().Properties))
                        {
                            if (foreignKey.PrincipalKey.IsPrimaryKey() &&
                                entityTypes.Contains(foreignKey.PrincipalEntityType))
                            {
                                var principalEntityType = foreignKey.PrincipalEntityType;
                                principalList.Add(principalEntityType);
                                if (!dependents.TryGetValue(principalEntityType, out dependentList))
                                {
                                    dependentList = new List <IEntityType>();
                                    dependents[principalEntityType] = dependentList;
                                }
                                ((List <IEntityType>)dependentList).Add(entityType);
                            }
                        }

                        principals[entityType] = principalList;
                    }

                    var stateManager = _currentContext.GetDependencies().StateManager;

                    ModificationCommandIdentityMap CommandIdentityMapFactory(
                        string name,
                        string schema,
                        Func <string> generateParameterName,
                        bool sensitiveLoggingEnabled)
                    => new ModificationCommandIdentityMap(
                        stateManager,
                        principals,
                        dependents,
                        name,
                        schema,
                        generateParameterName,
                        sensitiveLoggingEnabled);

                    foreach (var entityType in entityTypes)
                    {
                        sharedTablesMap.Add(entityType, CommandIdentityMapFactory);
                    }
                }
            }

            _tableSharingIdentityMapFactories = sharedTablesMap;
            return(sharedTablesMap);
        }