コード例 #1
0
        public virtual DirectiveFiles GenerateRuntimeDirectives()
        {
            var generator = new DirectiveGenerator();
            var members   = new List <MemberInfo>();

            foreach (var contextType in FindContextTypes())
            {
                var contextTypeName = contextType.Key.GetTypeInfo().FullName;
                using (var context = CreateContext(contextType.Value))
                {
                    var services   = _servicesBuilder.Build(context);
                    var discoverer = services.GetRequiredService <RuntimeTypeDiscoverer>();

                    _logger.Value.LogDebug(CommandsStrings.BeginRuntimeTypeDiscovery(contextTypeName));
                    var start = members.Count;

                    var assemblies = new[]
                    {
                        typeof(EntityType).GetTypeInfo().Assembly,
                        typeof(RelationalDatabase).GetTypeInfo().Assembly,
                        context.GetInfrastructure()
                        .GetRequiredService <IDbContextServices>()
                        .DatabaseProviderServices
                        .GetType()
                        .GetTypeInfo()
                        .Assembly
                    };

                    members.AddRange(discoverer.Discover(assemblies));

                    _logger.Value.LogDebug(CommandsStrings.EndRuntimeTypeDiscovery(members.Count - start, contextTypeName));
                }
            }
            var xml = generator.GenerateXml(members);

            var filename = Path.Combine(_projectDir, "Properties", "Microsoft.EntityFrameworkCore.g.rd.xml");

            Directory.CreateDirectory(Path.GetDirectoryName(filename));

            _logger.Value.LogInformation(CommandsStrings.WritingDirectives(filename));

            File.WriteAllText(filename, xml);

            return(new DirectiveFiles {
                GeneratedFile = filename
            });
        }