public void Required_options_to_GenerateModel_are_not_null()
        {
            var generator = CreateServices()
                            .AddSingleton <IProviderCodeGeneratorPlugin, TestCodeGeneratorPlugin>()
                            .BuildServiceProvider()
                            .GetRequiredService <IModelCodeGenerator>();

            Assert.StartsWith(
                CoreStrings.ArgumentPropertyNull(nameof(ModelCodeGenerationOptions.ContextName), "options"),
                Assert.Throws <ArgumentException>(
                    () =>
                    generator.GenerateModel(
                        new Model(),
                        new ModelCodeGenerationOptions
            {
                ContextName      = null,
                ConnectionString = "Initial Catalog=TestDatabase"
            })).Message);

            Assert.StartsWith(
                CoreStrings.ArgumentPropertyNull(nameof(ModelCodeGenerationOptions.ConnectionString), "options"),
                Assert.Throws <ArgumentException>(
                    () =>
                    generator.GenerateModel(
                        new Model(),
                        new ModelCodeGenerationOptions
            {
                ContextName      = "TestDbContext",
                ConnectionString = null
            })).Message);
        }
Beispiel #2
0
        public override ScaffoldedModel GenerateModel(
            IModel model,
            ModelCodeGenerationOptions options)
        {
            Check.NotNull(model, nameof(model));
            Check.NotNull(options, nameof(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));
            }

            if (options.ModelNamespace == null)
            {
                throw new ArgumentException(CoreStrings.ArgumentPropertyNull(nameof(options.ModelNamespace), nameof(options)), nameof(options));
            }

            var generatedCode = CSharpDbContextGenerator.WriteCode(
                model,
                options.ContextName,
                options.ConnectionString,
                options.ContextNamespace,
                options.ModelNamespace,
                options.UseDataAnnotations,
                options.SuppressConnectionStringWarning,
                options.SuppressOnConfiguring);

            // output DbContext .cs file
            var dbContextFileName = options.ContextName + FileExtension;
            var resultingFiles    = new ScaffoldedModel
            {
                ContextFile = new ScaffoldedFile
                {
                    Path = options.ContextDir != null
                        ? Path.Combine(options.ContextDir, dbContextFileName)
                        : dbContextFileName,
                    Code = generatedCode
                }
            };

            foreach (var entityType in model.GetEntityTypes())
            {
                generatedCode = CSharpEntityTypeGenerator.WriteCode(entityType, options.ModelNamespace, options.UseDataAnnotations);

                // output EntityType poco .cs file
                var entityTypeFileName = entityType.DisplayName() + FileExtension;
                resultingFiles.AdditionalFiles.Add(
                    new ScaffoldedFile {
                    Path = entityTypeFileName, Code = generatedCode
                });
            }

            return(resultingFiles);
        }
        public static string NullButNotEmpty(string value, [InvokerParameterName][NotNull] string parameterName)
        {
            if (value is object &&
                value.Length == 0)
            {
                NotEmpty(parameterName, nameof(parameterName));

                throw new ArgumentException(CoreStrings.ArgumentPropertyNull(value, parameterName));
            }

            return(value);
        }
        public static IReadOnlyList <T> NotEmpty <T>(IReadOnlyList <T> value, [InvokerParameterName][NotNull] string parameterName)
        {
            NotNull(value, parameterName);

            if (value.Count == 0)
            {
                NotEmpty(parameterName, nameof(parameterName));

                throw new ArgumentException(CoreStrings.ArgumentPropertyNull(value, parameterName));
            }

            return(value);
        }
        public void Required_options_to_GenerateModel_are_not_null()
        {
            var services = new ServiceCollection()
                           .AddEntityFrameworkDesignTimeServices();

            new SqlServerDesignTimeServices().ConfigureDesignTimeServices(services);
            services.AddSingleton <IProviderCodeGeneratorPlugin, TestCodeGeneratorPlugin>();

            var generator = services
                            .BuildServiceProvider()
                            .GetRequiredService <IModelCodeGenerator>();

            Assert.StartsWith(
                CoreStrings.ArgumentPropertyNull(nameof(ModelCodeGenerationOptions.ModelNamespace), "options"),
                Assert.Throws <ArgumentException>(
                    () =>
                    generator.GenerateModel(
                        new Model(),
                        new ModelCodeGenerationOptions
            {
                ModelNamespace   = null,
                ContextName      = "TestDbContext",
                ConnectionString = "Initial Catalog=TestDatabase"
            })).Message);

            Assert.StartsWith(
                CoreStrings.ArgumentPropertyNull(nameof(ModelCodeGenerationOptions.ContextName), "options"),
                Assert.Throws <ArgumentException>(
                    () =>
                    generator.GenerateModel(
                        new Model(),
                        new ModelCodeGenerationOptions
            {
                ModelNamespace   = "TestNamespace",
                ContextName      = null,
                ConnectionString = "Initial Catalog=TestDatabase"
            })).Message);

            Assert.StartsWith(
                CoreStrings.ArgumentPropertyNull(nameof(ModelCodeGenerationOptions.ConnectionString), "options"),
                Assert.Throws <ArgumentException>(
                    () =>
                    generator.GenerateModel(
                        new Model(),
                        new ModelCodeGenerationOptions
            {
                ModelNamespace   = "TestNamespace",
                ContextName      = "TestDbContext",
                ConnectionString = null
            })).Message);
        }
Beispiel #6
0
        public static T NotNull <T>(
            [NoEnumeration] T value,
            [InvokerParameterName][NotNull] string parameterName,
            [NotNull] string propertyName)
        {
            if (ReferenceEquals(value, null))
            {
                NotEmpty(parameterName, nameof(parameterName));
                NotEmpty(propertyName, nameof(propertyName));

                throw new ArgumentException(CoreStrings.ArgumentPropertyNull(propertyName, parameterName));
            }

            return(value);
        }
        public static string NotEmpty(string value, [InvokerParameterName][NotNull] string parameterName)
        {
            Exception e = null;

            if (value is null)
            {
                e = new ArgumentNullException(parameterName);
            }
            else if (value.Trim().Length == 0)
            {
                e = new ArgumentException(CoreStrings.ArgumentPropertyNull(value, parameterName));
            }

            if (e != null)
            {
                NotEmpty(parameterName, nameof(parameterName));

                throw e;
            }

            return(value);
        }
    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);
    }