protected virtual IDictionary <string, object> AddTemplateParameters( CodeType dbContextType, ModelMetadata modelMetadata) { if (dbContextType == null) { throw new ArgumentNullException("dbContextType"); } if (modelMetadata == null) { throw new ArgumentNullException("modelMetadata"); } if (String.IsNullOrEmpty(Model.ControllerName)) { throw new InvalidOperationException(Resources.InvalidControllerName); } IDictionary <string, object> templateParameters = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); CodeType modelType = Model.ModelType.CodeType; templateParameters.Add("ModelMetadata", modelMetadata); string modelTypeNamespace = modelType.Namespace != null ? modelType.Namespace.FullName : String.Empty; templateParameters.Add("ModelTypeNamespace", modelTypeNamespace); HashSet <string> requiredNamespaces = GetRequiredNamespaces(new List <CodeType>() { modelType, dbContextType }); templateParameters.Add("RequiredNamespaces", requiredNamespaces); templateParameters.Add("ModelTypeName", modelType.Name); templateParameters.Add("ContextTypeName", dbContextType.Name); templateParameters.Add("UseAsync", Model.IsAsyncSelected); CodeDomProvider provider = ValidationUtil.GenerateCodeDomProvider(Model.ActiveProject.GetCodeLanguage()); string modelVariable = provider.CreateEscapedIdentifier(Model.ModelType.ShortTypeName.ToLowerInvariantFirstChar()); templateParameters.Add("ModelVariable", modelVariable); templateParameters.Add("EntitySetVariable", modelMetadata.EntitySetName.ToLowerInvariantFirstChar()); // Overposting protection is only for MVC - and only when the model doesn't already have [Bind] if (Model.IsViewGenerationSupported) { bool isOverpostingProtectionRequired = OverpostingProtection.IsOverpostingProtectionRequired(modelType); templateParameters.Add("IsOverpostingProtectionRequired", isOverpostingProtectionRequired); if (isOverpostingProtectionRequired) { templateParameters.Add("OverpostingWarningMessage", OverpostingProtection.WarningMessage); templateParameters.Add("BindAttributeIncludeText", OverpostingProtection.GetBindAttributeIncludeText(modelMetadata)); } } return(templateParameters); }
/// <summary> /// Function generates the default DataContext name. /// </summary> public string GenerateDefaultDataContextTypeName() { Project project = ActiveProject; string modelsNamespace = MvcProjectUtil.GetDefaultModelsNamespace(project.GetDefaultNamespace()); CodeDomProvider provider = ValidationUtil.GenerateCodeDomProvider(project.GetCodeLanguage()); // CreateValidIdentifier considers dot as a valid identifier hence replacing the dot explicitly. string projectName = provider.CreateValidIdentifier(project.GetDefaultNamespace().Replace(".", String.Empty)); DataContextName = modelsNamespace + "." + projectName + MvcProjectUtil.DataContextSuffix; return(DataContextName); }
protected override void AddTemplateParameters(IDictionary <string, object> templateParameters) { base.AddTemplateParameters(templateParameters); CodeType codeType = Model.ModelType.CodeType; ModelMetadata metadata = new CodeModelModelMetadata(codeType); if (metadata.PrimaryKeys.Length == 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.NoKeyDefinedError, codeType.Name)); } templateParameters.Add("ModelMetadata", metadata); HashSet <string> requiredNamespaces = GetRequiredNamespaces(new List <CodeType>() { codeType }); templateParameters.Add("RequiredNamespaces", requiredNamespaces); string modelTypeNamespace = codeType.Namespace != null ? codeType.Namespace.FullName : String.Empty; templateParameters.Add("ModelTypeNamespace", modelTypeNamespace); templateParameters.Add("ModelTypeName", codeType.Name); templateParameters.Add("UseAsync", Model.IsAsyncSelected); IEntityFrameworkService efService = Context.ServiceProvider.GetService <IEntityFrameworkService>(); string entitySetName = efService.GetPluralizedWord(codeType.Name, CultureInfo.InvariantCulture); templateParameters.Add("EntitySetName", entitySetName); CodeDomProvider provider = ValidationUtil.GenerateCodeDomProvider(Model.ActiveProject.GetCodeLanguage()); string modelVariable = provider.CreateEscapedIdentifier(codeType.Name.ToLowerInvariantFirstChar()); string entitySetVariable = provider.CreateEscapedIdentifier(entitySetName.ToLowerInvariantFirstChar()); templateParameters.Add("ModelVariable", modelVariable); templateParameters.Add("EntitySetVariable", entitySetVariable); templateParameters.Add("ODataModificationMessage", Resources.ScaffoldODataModificationMessage); templateParameters.Add("IsLegacyOdataVersion", Framework.IsODataLegacy(Context)); }