EFLinqAnalyzer.EFUsageContext.Build C# (CSharp) Метод

Build() публичный Метод

Builds the EF-specific view of the current semantic model
public Build ( ) : bool
Результат bool
        public bool Build()
        {
            //Bail immediately if EntityFramework is not referenced
            if (!_context.SemanticModel
                         .Compilation
                         .ReferencedAssemblyNames
                         .Any(asm => asm.Name == "EntityFramework" 
                                  && asm.Version.Major == 6))
            {
                return false;
            }
            
            var typeSymbols = _context.SemanticModel
                                      .LookupSymbols(_context.Node
                                                             .GetLocation()
                                                             .SourceSpan
                                                             .Start)
                                      .OfType<INamedTypeSymbol>();

            var symbols = typeSymbols.Where(t => t.UltimatelyDerivesFromDbContext());

            _dbContextSymbols = new ReadOnlyCollection<INamedTypeSymbol>(symbols.ToList());

            if (_dbContextSymbols.Count == 0)
                return false;

            //TODO: Need to follow any related types that hang off of the primary entities (referenced via DbSet<T>)
            var entityTypes = _dbContextSymbols.SelectMany(dbc =>
            {
                var dbSetProps = dbc.GetMembers()
                    .Where(m => m.Kind == SymbolKind.Property)
                    .Cast<IPropertySymbol>()
                    .Where(t => t.IsDbSetProperty());

                return dbSetProps;
            })
            .Select(p => p.Type)
            .OfType<INamedTypeSymbol>()
            .Where(t => t.TypeArguments.Length == 1)
            .Select(t => t.TypeArguments.First())
            .OfType<INamedTypeSymbol>();

            _entityTypeSymbols = new ReadOnlyCollection<INamedTypeSymbol>(entityTypes.ToList());

            _clsInfo = new Dictionary<ITypeSymbol, EFCodeFirstClassInfo>();
            _propertiesToCls = new Dictionary<string, List<EFCodeFirstClassInfo>>();
            foreach (var clsSymbol in _entityTypeSymbols)
            {
                var clsSymbols = clsSymbol.GetMembers()
                                              .Where(m => m.Kind == SymbolKind.Property)
                                              .Cast<IPropertySymbol>();
                var clsInfo = new EFCodeFirstClassInfo(clsSymbol);
                clsInfo.AddProperties(clsSymbols, (sym) =>
                {
                    if (!_propertiesToCls.ContainsKey(sym.Name))
                        _propertiesToCls[sym.Name] = new List<EFCodeFirstClassInfo>();
                    _propertiesToCls[sym.Name].Add(clsInfo);
                });

                _clsInfo[clsSymbol] = clsInfo;
            }
            return _clsInfo.Count > 0;
        }

Usage Example

        private static void AnalyzeLinqExpression(SyntaxNodeAnalysisContext context)
        {
            var efContext = new EFUsageContext(context);

            //Found nothing, don't bother continuing
            if (!efContext.Build())
            {
                return;
            }

            //Check if the lambda is part of an IQueryable call chain. If so, check that it only contains valid
            //EF LINQ constructs (initializers, entity members, entity navigation properties)
            var query  = context.Node as QueryExpressionSyntax;
            var lambda = context.Node as LambdaExpressionSyntax;

            if (query != null)
            {
                AnalyzeQueryExpression(context, efContext, query);
            }
            else if (lambda != null)
            {
                AnalyzeLambdaInLinqExpression(context, efContext, lambda);
            }
        }
All Usage Examples Of EFLinqAnalyzer.EFUsageContext::Build