コード例 #1
0
        // Test stopped working with 15.6 Preview 7 - plan to re-enable with https://github.com/aspnet/EntityFramework6/issues/541
        // [Fact]
        public void Generate_throws_when_error_in_entity_type_template()
        {
            var project = MockDTE.CreateProject();
            var token   = Guid.NewGuid().ToString();

            using (AddCustomizedTemplates(project))
            {
                var templatePath = Path.GetTempFileName();
                try
                {
                    File.WriteAllText(templatePath, "<# throw new Exception(\"" + token + "\"); #>");

                    var fullPathProperty = project.ProjectItems.Item("CodeTemplates").ProjectItems
                                           .Item("EFModelFromDatabase").ProjectItems.Item("EntityType.cs.t4").Properties.Cast <Property>()
                                           .First(i => i.Name == "FullPath");
                    Mock.Get(fullPathProperty).SetupGet(p => p.Value).Returns(templatePath);

                    var generator = new CodeFirstModelGenerator(project);

                    var ex = Assert.Throws <CodeFirstModelGenerationException>(
                        () => generator.Generate(Model, "WebApplication1.Models", "MyContext", "MyContextConnString")
                        .ToArray());

                    Assert.Equal(
                        string.Format(Resources.ErrorGeneratingCodeFirstModel, "Entity.cs"),
                        ex.Message);
                    Assert.Contains(token, ex.InnerException.Message);
                }
                finally
                {
                    File.Delete(templatePath);
                }
            }
        }
コード例 #2
0
        public void Generate_returns_code_when_vb()
        {
            var generator = new CodeFirstModelGenerator(MockDTE.CreateProject(kind: MockDTE.VBProjectKind));

            var files = generator.Generate(Model, "WebApplication1.Models", "MyContext", "MyContextConnString").ToArray();

            Assert.Equal(2, files.Length);
            Assert.Equal(new[] { "MyContext.vb", "Entity.vb" }, files.Select(p => p.Key));
        }
コード例 #3
0
        public void Generate_returns_code_when_vb()
        {
            var generator = new CodeFirstModelGenerator(MockDTE.CreateProject(kind: MockDTE.VBProjectKind));

            var files = generator.Generate(Model, "WebApplication1.Models", "MyContext", "MyContextConnString").ToArray();

            Assert.Equal(2, files.Length);
            Assert.Equal(new[] { "MyContext.vb", "Entity.vb" }, files.Select(p => p.Key));
        }
コード例 #4
0
        public void Generate_returns_code_when_vb_and_empty_model()
        {
            var generator = new CodeFirstModelGenerator(MockDTE.CreateProject(kind: MockDTE.VBProjectKind));

            var files = generator.Generate(null, "WebApplication1.Models", "MyContext", "MyContextConnString").ToArray();

            Assert.Equal(1, files.Length);
            Assert.Equal("MyContext.vb", files[0].Key);
            Assert.Contains(Resources.CodeFirstCodeFile_DbSetComment_VB, files[0].Value);
        }
コード例 #5
0
        public void Generate_returns_code_when_vb_and_empty_model()
        {
            var generator = new CodeFirstModelGenerator(MockDTE.CreateProject(kind: MockDTE.VBProjectKind));

            var files = generator.Generate(null, "WebApplication1.Models", "MyContext", "MyContextConnString").ToArray();

            Assert.Equal(1, files.Length);
            Assert.Equal("MyContext.vb", files[0].Key);
            Assert.Contains(Resources.CodeFirstCodeFile_DbSetComment_VB, files[0].Value);
        }
コード例 #6
0
        // Test stopped working with 15.6 Preview 7 - plan to re-enable with https://github.com/aspnet/EntityFramework6/issues/541
        // [Fact]
        public void Generate_returns_code_when_cs_and_customized()
        {
            var project = MockDTE.CreateProject();

            using (AddCustomizedTemplates(project))
            {
                var generator = new CodeFirstModelGenerator(project);

                var files = generator.Generate(Model, "WebApplication1.Models", "MyContext", "MyContextConnString").ToArray();

                Assert.Equal(2, files.Length);
                Assert.Equal(new[] { "MyContext.cs", "Entity.cs" }, files.Select(p => p.Key));
                Assert.True(files.All(p => p.Value == "Customized"));
            }
        }
コード例 #7
0
        public void Generate_returns_code_when_vb_and_customized()
        {
            var project = MockDTE.CreateProject(kind: MockDTE.VBProjectKind);

            using (AddCustomizedTemplates(project))
            {
                var generator = new CodeFirstModelGenerator(project);

                var files = generator.Generate(Model, "WebApplication1.Models", "MyContext", "MyContextConnString").ToArray();

                Assert.Equal(2, files.Length);
                Assert.Equal(new[] { "MyContext.vb", "Entity.vb" }, files.Select(p => p.Key));
                Assert.True(files.All(p => p.Value == "Customized"));
            }
        }
コード例 #8
0
        public void Generate_throws_when_error_in_entity_type_template()
        {
            var project = MockDTE.CreateProject();
            var token = Guid.NewGuid().ToString();

            using (AddCustomizedTemplates(project))
            {
                var templatePath = Path.GetTempFileName();
                try
                {
                    File.WriteAllText(templatePath, "<# throw new Exception(\"" + token + "\"); #>");

                    var fullPathProperty = project.ProjectItems.Item("CodeTemplates").ProjectItems
                        .Item("EFModelFromDatabase").ProjectItems.Item("EntityType.cs.t4").Properties.Cast<Property>()
                        .First(i => i.Name == "FullPath");
                    Mock.Get(fullPathProperty).SetupGet(p => p.Value).Returns(templatePath);

                    var generator = new CodeFirstModelGenerator(project);

                    var ex = Assert.Throws<CodeFirstModelGenerationException>(
                        () => generator.Generate(Model, "WebApplication1.Models", "MyContext", "MyContextConnString")
                            .ToArray());

                    Assert.Equal(
                        string.Format(Resources.ErrorGeneratingCodeFirstModel, "Entity.cs"),
                        ex.Message);
                    Assert.Contains(token, ex.InnerException.Message);
                }
                finally
                {
                    File.Delete(templatePath);
                }
            }
        }
コード例 #9
0
        internal void RunStarted(ModelBuilderSettings modelBuilderSettings, CodeFirstModelGenerator codeFirstModelGenerator, Dictionary<string, string> replacementsDictionary)
        {
            try
            {
                var contextClassName =
                    new CodeIdentifierUtils(
                        modelBuilderSettings.VSApplicationType,
                        _vsUtils.GetLanguageForProject(modelBuilderSettings.Project))
                        .CreateValidIdentifier(replacementsDictionary["$safeitemname$"]);

                _generatedCode = codeFirstModelGenerator.Generate(
                    modelBuilderSettings.ModelBuilderEngine != null
                            ? modelBuilderSettings.ModelBuilderEngine.Model
                            : null,
                    replacementsDictionary["$rootnamespace$"],
                    contextClassName,
                    modelBuilderSettings.SaveConnectionStringInAppConfig
                        ? modelBuilderSettings.AppConfigConnectionPropertyName
                        : contextClassName).ToList();

                Debug.Assert(_generatedCode.Count > 0, "code has not been generated");

                replacementsDictionary["$contextfilecontents$"] = _generatedCode[0].Value;
            }
            catch (CodeFirstModelGenerationException e)
            {
                _vsUtils.ShowErrorDialog(
                    string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}", e.Message, Environment.NewLine, e.InnerException));
            }
        }