public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationOptions options) { if (options.ContextName == null) { throw new ArgumentException( CoreStrings.ArgumentPropertyNull(nameof(options.ContextName), nameof(options)), nameof(options)); } if (options.ConnectionString == null) { throw new ArgumentException( CoreStrings.ArgumentPropertyNull(nameof(options.ConnectionString), nameof(options)), nameof(options)); } var resultingFiles = new ScaffoldedModel(); var contextTemplate = Path.Combine(options.ProjectDir !, TemplatesDirectory, "DbContext.t4"); Check.DebugAssert(_host.Session == null, "Session is not null."); _host.Session = _host.CreateSession(); try { _host.Session.Add("Model", model); _host.Session.Add("Options", options); _host.Session.Add("NamespaceHint", options.ContextNamespace ?? options.ModelNamespace); _host.Session.Add("ProjectDefaultNamespace", options.RootNamespace); var handler = new TextTemplatingCallback(); var generatedCode = ProcessTemplate(contextTemplate, handler); var dbContextFileName = options.ContextName + handler.Extension; resultingFiles.ContextFile = new ScaffoldedFile { Path = options.ContextDir != null ? Path.Combine(options.ContextDir, dbContextFileName) : dbContextFileName, Code = generatedCode }; } finally { _host.Session = null; } var entityTypeTemplate = Path.Combine(options.ProjectDir !, TemplatesDirectory, "EntityType.t4"); if (File.Exists(entityTypeTemplate)) { foreach (var entityType in model.GetEntityTypes()) { // TODO: Should this be handled inside the template? if (CSharpDbContextGenerator.IsManyToManyJoinEntityType(entityType)) { continue; } _host.Session = _host.CreateSession(); try { _host.Session.Add("EntityType", entityType); _host.Session.Add("Options", options); _host.Session.Add("NamespaceHint", options.ModelNamespace); _host.Session.Add("ProjectDefaultNamespace", options.RootNamespace); var handler = new TextTemplatingCallback(); var generatedCode = ProcessTemplate(entityTypeTemplate, handler); if (string.IsNullOrWhiteSpace(generatedCode)) { continue; } var entityTypeFileName = entityType.Name + handler.Extension; resultingFiles.AdditionalFiles.Add( new ScaffoldedFile { Path = entityTypeFileName, Code = generatedCode }); } finally { _host.Session = null; } } } return(resultingFiles); }