コード例 #1
0
        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);
        }
コード例 #2
0
        /// <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);
        }