public void GetAttributeBody_returns_body_when_order_and_type_and_vb()
        {
            var configuration = new ColumnConfiguration { Order = 0, TypeName = "int" };
            var code = new VBCodeHelper();

            Assert.Equal("Column(Order:=0, TypeName:=\"int\")", configuration.GetAttributeBody(code));
        }
Example #2
0
        public void Literal_returns_bool()
        {
            var code = new VBCodeHelper();

            Assert.Equal("True", code.Literal(true));
            Assert.Equal("False", code.Literal(false));
        }
Example #3
0
        public void Lambda_returns_property_accessor_when_one()
        {
            var code   = new VBCodeHelper();
            var member = Model.ConceptualModel.EntityTypes.First().Properties.First(p => p.Name == "Id");

            Assert.Equal("Function(e) e.Id", code.Lambda(member));
        }
        public void Literal_returns_bool()
        {
            var code = new VBCodeHelper();

            Assert.Equal("True", code.Literal(true));
            Assert.Equal("False", code.Literal(false));
        }
Example #5
0
        public void Type_returns_collection_property_type()
        {
            var property = Model.ConceptualModel.EntityTypes.First().NavigationProperties.First(
                p => p.Name == "Children");
            var code = new VBCodeHelper();

            Assert.Equal("ICollection(Of Entity)", code.Type(property));
        }
        public void Attribute_surrounds_body()
        {
            var code = new VBCodeHelper();
            var configuration = new Mock<IAttributeConfiguration>();
            configuration.Setup(c => c.GetAttributeBody(code)).Returns("Required");

            Assert.Equal("<Required>", code.Attribute(configuration.Object));
        }
Example #7
0
        public void Lambda_returns_anonymous_type_when_one()
        {
            var code = new VBCodeHelper();
            var id   = Model.ConceptualModel.EntityTypes.First().Properties.First(p => p.Name == "Id");
            var name = Model.ConceptualModel.EntityTypes.First().Properties.First(p => p.Name == "Name");

            Assert.Equal("Function(e) New With {e.Id, e.Name}", code.Lambda(new[] { id, name }));
        }
        public void Type_escapes_container_name()
        {
            var container = new Mock<EntityContainer>();
            container.SetupGet(c => c.Name).Returns("Nothing");
            var code = new VBCodeHelper();

            Assert.Equal("_Nothing", code.Type(container.Object));
        }
        public void Type_returns_collection_property_type()
        {
            var property = Model.ConceptualModel.EntityTypes.First().NavigationProperties.First(
                p => p.Name == "Children");
            var code = new VBCodeHelper();

            Assert.Equal("ICollection(Of Entity)", code.Type(property));
        }
        public void Type_returns_property_type()
        {
            var property = Model.ConceptualModel.EntityTypes.First().Properties.First(
                p => p.Name == "Name");
            var code = new VBCodeHelper();

            Assert.Equal("String", code.Type(property));
        }
Example #11
0
        public void Type_returns_property_type()
        {
            var property = Model.ConceptualModel.EntityTypes.First().Properties.First(
                p => p.Name == "Name");
            var code = new VBCodeHelper();

            Assert.Equal("String", code.Type(property));
        }
Example #12
0
        public void Type_escapes_container_name()
        {
            var container = new Mock <EntityContainer>();

            container.SetupGet(c => c.Name).Returns("Nothing");
            var code = new VBCodeHelper();

            Assert.Equal("_Nothing", code.Type(container.Object));
        }
Example #13
0
        public void GetAttributeBody_returns_body_when_order_and_type_and_vb()
        {
            var configuration = new ColumnConfiguration {
                Order = 0, TypeName = "int"
            };
            var code = new VBCodeHelper();

            Assert.Equal("Column(Order:=0, TypeName:=\"int\")", configuration.GetAttributeBody(code));
        }
Example #14
0
        public void Attribute_surrounds_body()
        {
            var code          = new VBCodeHelper();
            var configuration = new Mock <IAttributeConfiguration>();

            configuration.Setup(c => c.GetAttributeBody(code)).Returns("Required");

            Assert.Equal("<Required>", code.Attribute(configuration.Object));
        }
Example #15
0
        public void TypeArgument_surrounds_value()
        {
            var code = new VBCodeHelper();

            Assert.Equal("(Of Entity)", code.TypeArgument("Entity"));
        }
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {

    var code = new VBCodeHelper();
    var edm = new EdmHelper(code);

    if (Model == null)
    {
        throw new ArgumentNullException("Model");
    }

    if (Namespace == null)
    {
        throw new ArgumentNullException("Namespace");
    }

    if (ContextClassName == null)
    {
        throw new ArgumentNullException("ContextClassName");
    }

    if (ConnectionStringName == null)
    {
        throw new ArgumentNullException("ConnectionStringName");
    }

            this.Write("Imports System\r\nImports System.Data.Entity\r\nImports System.ComponentModel.DataAnn" +
                    "otations.Schema\r\nImports System.Linq\r\n\r\nPartial Public Class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ContextClassName));
            this.Write("\r\n    Inherits DbContext\r\n\r\n    Public Sub New()\r\n        MyBase.New(\"name=");
            this.Write(this.ToStringHelper.ToStringWithCulture(ConnectionStringName));
            this.Write("\")        \r\n    End Sub\r\n\r\n");

    foreach (var entitySet in Model.ConceptualModel.Container.EntitySets)
    {

            this.Write("    Public Overridable Property ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(entitySet)));
            this.Write(" As DbSet(Of ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType)));
            this.Write(")\r\n");

    }

            this.Write("\r\n    Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilde" +
                    "r)\r\n");

    var anyConfiguration = false;

    foreach (var entitySet in Model.ConceptualModel.Container.EntitySets)
    {
        var typeConfigurations = edm.GetConfigurations(entitySet, Model).OfType<IFluentConfiguration>()
            .Where(c => !(c is IAttributeConfiguration || c is KeyConfiguration));

        var firstTypeConfiguration = true;
        foreach (var typeConfiguration in typeConfigurations)
        {
            if (firstTypeConfiguration)
            {
                firstTypeConfiguration = false;

                if (anyConfiguration)
                {
                    WriteLine(string.Empty);
                }
                else
                {
                    anyConfiguration = true;
                }


            this.Write("        modelBuilder.Entity(Of ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType)));
            this.Write(")() _\r\n");

            }
            else
            {
                WriteLine(" _");
            }

            Write("            " + code.MethodChain(typeConfiguration));
        }

        if (!firstTypeConfiguration)
        {
            WriteLine(string.Empty);
        }

        foreach (var property in entitySet.ElementType.Properties)
        {
            var propertyConfigurations = edm.GetConfigurations(property, Model).OfType<IFluentConfiguration>()
                .Where(c => !(c is IAttributeConfiguration));

            var firstPropertyConfiguration = true;
            foreach (var propertyConfiguration in propertyConfigurations)
            {
                var columnConfiguration = propertyConfiguration as ColumnConfiguration;
                if (columnConfiguration != null)
                {
                    // Unset this since it is implied in the key configuration calls themselves
                    columnConfiguration.Order = null;

                    if (columnConfiguration.Name == null && columnConfiguration.TypeName == null)
                    {
                        // Nothing left to configure
                        continue;
                    }
                }

                if (firstPropertyConfiguration)
                {
                    firstPropertyConfiguration = false;
                    
                    if (anyConfiguration)
                    {
                        WriteLine(string.Empty);
                    }
                    else
                    {
                        anyConfiguration = true;
                    }


            this.Write("        modelBuilder.Entity(Of ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType)));
            this.Write(")() _\r\n            .Property(Function(e) e.");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(property)));
            this.Write(") _\r\n");

                }
                else
                {
                    WriteLine(" _");
                }

                Write("            " + code.MethodChain(propertyConfiguration));
            }

            if (!firstPropertyConfiguration)
            {
                WriteLine(string.Empty);
            }
        }

        foreach (var navigationProperty in entitySet.ElementType.NavigationProperties)
        {
            // Only configure relationships from one end
            if (navigationProperty.RelationshipType.RelationshipEndMembers.First() != navigationProperty.FromEndMember)
            {
                continue;
            }

            bool isDefaultMultiplicity;
            var navigationPropertyMultiplicityConfiguration = edm.GetMultiplicityConfiguration(navigationProperty, out isDefaultMultiplicity);
            var navigationPropertyConfigurations = edm.GetConfigurations(navigationProperty, Model);

            var firstNavigationPropertyConfiguration = true;
            foreach (var navigationPropertyConfiguration in navigationPropertyConfigurations)
            {
                if (firstNavigationPropertyConfiguration)
                {
                    firstNavigationPropertyConfiguration = false;
                    
                    if (anyConfiguration)
                    {
                        WriteLine(string.Empty);
                    }
                    else
                    {
                        anyConfiguration = true;
                    }


            this.Write("        modelBuilder");
            this.Write(this.ToStringHelper.ToStringWithCulture(ApplyVBFixup(code.MethodChain(navigationPropertyMultiplicityConfiguration))));
            this.Write(" _\r\n");

                }
                else
                {
                    WriteLine(" _");
                }

                Write("            " + code.MethodChain(navigationPropertyConfiguration));
            }

            if (!firstNavigationPropertyConfiguration)
            {
                WriteLine(string.Empty);
            }
            else if (!isDefaultMultiplicity)
            {            
                if (anyConfiguration)
                {
                    WriteLine(string.Empty);
                }
                else
                {
                    anyConfiguration = true;
                }

            this.Write("        modelBuilder");
            this.Write(this.ToStringHelper.ToStringWithCulture(ApplyVBFixup(code.MethodChain(navigationPropertyMultiplicityConfiguration))));
            this.Write("\r\n");

            }
        }
    }

            this.Write("    End Sub\r\nEnd Class\r\n");
            return this.GenerationEnvironment.ToString();
        }
Example #17
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            var code = new VBCodeHelper();
            var edm  = new EdmHelper(code);

            if (EntitySet == null)
            {
                throw new ArgumentNullException("EntitySet");
            }

            if (Model == null)
            {
                throw new ArgumentNullException("Model");
            }

            var entityType = EntitySet.ElementType;

            this.Write("Imports System\r\nImports System.Collections.Generic\r\nImports System.ComponentModel" +
                       ".DataAnnotations\r\nImports System.ComponentModel.DataAnnotations.Schema\r\nImports " +
                       "System.Data.Entity.Spatial\r\n\r\n");

            var typeConfigurations = edm.GetConfigurations(EntitySet, Model).OfType <IAttributeConfiguration>();

            foreach (var typeConfiguration in typeConfigurations)
            {
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Attribute(typeConfiguration)));
                this.Write("\r\n");
            }

            this.Write("Partial Public Class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entityType)));
            this.Write("\r\n");

            var collectionProperties = from p in entityType.NavigationProperties
                                       where p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many
                                       select p;

            if (collectionProperties.Any())
            {
                this.Write("    Public Sub New()\r\n");

                foreach (var collectionProperty in collectionProperties)
                {
                    this.Write("        ");
                    this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(collectionProperty)));
                    this.Write(" = New HashSet(Of ");
                    this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(collectionProperty.ToEndMember.GetEntityType())));
                    this.Write(")()\r\n");
                }

                this.Write("    End Sub\r\n\r\n");
            }

            var first = true;

            foreach (var property in entityType.Properties)
            {
                if (!first)
                {
                    WriteLine(string.Empty);
                }
                else
                {
                    first = false;
                }

                var propertyConfigurations = edm.GetConfigurations(property, Model).OfType <IAttributeConfiguration>();

                foreach (var propertyConfiguration in propertyConfigurations)
                {
                    this.Write("    ");
                    this.Write(this.ToStringHelper.ToStringWithCulture(code.Attribute(propertyConfiguration)));
                    this.Write("\r\n");
                }

                this.Write("    Public Property ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(property)));
                this.Write(" As ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(property)));
                this.Write("\r\n");
            }

            foreach (var navigationProperty in entityType.NavigationProperties)
            {
                if (!first)
                {
                    WriteLine(string.Empty);
                }
                else
                {
                    first = false;
                }


                this.Write("    Public Overridable Property ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(navigationProperty)));
                this.Write(" As ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(navigationProperty)));
                this.Write("\r\n");
            }

            this.Write("End Class\r\n");
            return(this.GenerationEnvironment.ToString());
        }
        public void Lambda_returns_anonymous_type_when_one()
        {
            var code = new VBCodeHelper();
            var id = Model.ConceptualModel.EntityTypes.First().Properties.First(p => p.Name == "Id");
            var name = Model.ConceptualModel.EntityTypes.First().Properties.First(p => p.Name == "Name");

            Assert.Equal("Function(e) New With {e.Id, e.Name}", code.Lambda(new[] { id, name }));
        }
        public void Literal_returns_string_array_when_more_than_one()
        {
            var code = new VBCodeHelper();

            Assert.Equal("{\"One\", \"Two\"}", code.Literal(new[] { "One", "Two" }));
        }
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {

    var code = new VBCodeHelper();
    var edm = new EdmHelper(code);

    if (EntitySet == null)
    {
        throw new ArgumentNullException("EntitySet");
    }

    if (Model == null)
    {
        throw new ArgumentNullException("Model");
    }

    var entityType = EntitySet.ElementType;

            this.Write("Imports System\r\nImports System.Collections.Generic\r\nImports System.ComponentModel" +
                    ".DataAnnotations\r\nImports System.ComponentModel.DataAnnotations.Schema\r\nImports " +
                    "System.Data.Entity.Spatial\r\n\r\n");

    var typeConfigurations = edm.GetConfigurations(EntitySet, Model).OfType<IAttributeConfiguration>();

    foreach (var typeConfiguration in typeConfigurations)
    {

            this.Write(this.ToStringHelper.ToStringWithCulture(code.Attribute(typeConfiguration)));
            this.Write("\r\n");

    }

            this.Write("Partial Public Class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entityType)));
            this.Write("\r\n");

    var collectionProperties = from p in entityType.NavigationProperties
                               where p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many
                               select p;

    if (collectionProperties.Any())
    {

            this.Write("    Public Sub New()\r\n");

    foreach (var collectionProperty in collectionProperties)
    {

            this.Write("        ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(collectionProperty)));
            this.Write(" = New HashSet(Of ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(collectionProperty.ToEndMember.GetEntityType())));
            this.Write(")()\r\n");

    }

            this.Write("    End Sub\r\n\r\n");

    }

    var first = true;

    foreach (var property in entityType.Properties)
    {
        if (!first)
        {
            WriteLine(string.Empty);
        }
        else
        {
            first = false;
        }

        var propertyConfigurations = edm.GetConfigurations(property, Model).OfType<IAttributeConfiguration>();

        foreach (var propertyConfiguration in propertyConfigurations)
        {

            this.Write("    ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Attribute(propertyConfiguration)));
            this.Write("\r\n");

        }

            this.Write("    Public Property ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(property)));
            this.Write(" As ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(property)));
            this.Write("\r\n");

    }

    foreach (var navigationProperty in entityType.NavigationProperties)
    {
        if (!first)
        {
            WriteLine(string.Empty);
        }
        else
        {
            first = false;
        }


            this.Write("    Public Overridable Property ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(navigationProperty)));
            this.Write(" As ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(navigationProperty)));
            this.Write("\r\n");

    }

            this.Write("End Class\r\n");
            return this.GenerationEnvironment.ToString();
        }
        public void BeginLambda_returns_lambda_beginning()
        {
            var code = new VBCodeHelper();

            Assert.Equal("Function(x) ", code.BeginLambda("x"));
        }
        public void TypeArgument_surrounds_value()
        {
            var code = new VBCodeHelper();

            Assert.Equal("(Of Entity)", code.TypeArgument("Entity"));
        }
Example #23
0
        public void Literal_returns_string_array_when_more_than_one()
        {
            var code = new VBCodeHelper();

            Assert.Equal("{\"One\", \"Two\"}", code.Literal(new[] { "One", "Two" }));
        }
        public void Lambda_returns_property_accessor_when_one()
        {
            var code = new VBCodeHelper();
            var member = Model.ConceptualModel.EntityTypes.First().Properties.First(p => p.Name == "Id");

            Assert.Equal("Function(e) e.Id", code.Lambda(member));
        }
Example #25
0
        public void BeginLambda_returns_lambda_beginning()
        {
            var code = new VBCodeHelper();

            Assert.Equal("Function(x) ", code.BeginLambda("x"));
        }
Example #26
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            var code = new VBCodeHelper();
            var edm  = new EdmHelper(code);

            if (Model == null)
            {
                throw new ArgumentNullException("Model");
            }

            if (Namespace == null)
            {
                throw new ArgumentNullException("Namespace");
            }

            if (ContextClassName == null)
            {
                throw new ArgumentNullException("ContextClassName");
            }

            if (ConnectionStringName == null)
            {
                throw new ArgumentNullException("ConnectionStringName");
            }

            this.Write("Imports System\r\nImports System.Data.Entity\r\nImports System.ComponentModel.DataAnn" +
                       "otations.Schema\r\nImports System.Linq\r\n\r\nPartial Public Class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ContextClassName));
            this.Write("\r\n    Inherits DbContext\r\n\r\n    Public Sub New()\r\n        MyBase.New(\"name=");
            this.Write(this.ToStringHelper.ToStringWithCulture(ConnectionStringName));
            this.Write("\")        \r\n    End Sub\r\n\r\n");

            foreach (var entitySet in Model.ConceptualModel.Container.EntitySets)
            {
                this.Write("    Public Overridable Property ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(entitySet)));
                this.Write(" As DbSet(Of ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType)));
                this.Write(")\r\n");
            }

            this.Write("\r\n    Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilde" +
                       "r)\r\n");

            var anyConfiguration = false;

            foreach (var entitySet in Model.ConceptualModel.Container.EntitySets)
            {
                var typeConfigurations = edm.GetConfigurations(entitySet, Model).OfType <IFluentConfiguration>()
                                         .Where(c => !(c is IAttributeConfiguration || c is KeyConfiguration));

                var firstTypeConfiguration = true;
                foreach (var typeConfiguration in typeConfigurations)
                {
                    if (firstTypeConfiguration)
                    {
                        firstTypeConfiguration = false;

                        if (anyConfiguration)
                        {
                            WriteLine(string.Empty);
                        }
                        else
                        {
                            anyConfiguration = true;
                        }


                        this.Write("        modelBuilder.Entity(Of ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType)));
                        this.Write(")() _\r\n");
                    }
                    else
                    {
                        WriteLine(" _");
                    }

                    Write("            " + code.MethodChain(typeConfiguration));
                }

                if (!firstTypeConfiguration)
                {
                    WriteLine(string.Empty);
                }

                foreach (var property in entitySet.ElementType.Properties)
                {
                    var propertyConfigurations = edm.GetConfigurations(property, Model).OfType <IFluentConfiguration>()
                                                 .Where(c => !(c is IAttributeConfiguration));

                    var firstPropertyConfiguration = true;
                    foreach (var propertyConfiguration in propertyConfigurations)
                    {
                        var columnConfiguration = propertyConfiguration as ColumnConfiguration;
                        if (columnConfiguration != null)
                        {
                            // Unset this since it is implied in the key configuration calls themselves
                            columnConfiguration.Order = null;

                            if (columnConfiguration.Name == null && columnConfiguration.TypeName == null)
                            {
                                // Nothing left to configure
                                continue;
                            }
                        }

                        if (firstPropertyConfiguration)
                        {
                            firstPropertyConfiguration = false;

                            if (anyConfiguration)
                            {
                                WriteLine(string.Empty);
                            }
                            else
                            {
                                anyConfiguration = true;
                            }


                            this.Write("        modelBuilder.Entity(Of ");
                            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType)));
                            this.Write(")() _\r\n            .Property(Function(e) e.");
                            this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(property)));
                            this.Write(") _\r\n");
                        }
                        else
                        {
                            WriteLine(" _");
                        }

                        Write("            " + code.MethodChain(propertyConfiguration));
                    }

                    if (!firstPropertyConfiguration)
                    {
                        WriteLine(string.Empty);
                    }
                }

                foreach (var navigationProperty in entitySet.ElementType.NavigationProperties)
                {
                    // Only configure relationships from one end
                    if (navigationProperty.RelationshipType.RelationshipEndMembers.First() != navigationProperty.FromEndMember)
                    {
                        continue;
                    }

                    bool isDefaultMultiplicity;
                    var  navigationPropertyMultiplicityConfiguration = edm.GetMultiplicityConfiguration(navigationProperty, out isDefaultMultiplicity);
                    var  navigationPropertyConfigurations            = edm.GetConfigurations(navigationProperty, Model);

                    var firstNavigationPropertyConfiguration = true;
                    foreach (var navigationPropertyConfiguration in navigationPropertyConfigurations)
                    {
                        if (firstNavigationPropertyConfiguration)
                        {
                            firstNavigationPropertyConfiguration = false;

                            if (anyConfiguration)
                            {
                                WriteLine(string.Empty);
                            }
                            else
                            {
                                anyConfiguration = true;
                            }


                            this.Write("        modelBuilder");
                            this.Write(this.ToStringHelper.ToStringWithCulture(ApplyVBFixup(code.MethodChain(navigationPropertyMultiplicityConfiguration))));
                            this.Write(" _\r\n");
                        }
                        else
                        {
                            WriteLine(" _");
                        }

                        Write("            " + code.MethodChain(navigationPropertyConfiguration));
                    }

                    if (!firstNavigationPropertyConfiguration)
                    {
                        WriteLine(string.Empty);
                    }
                    else if (!isDefaultMultiplicity)
                    {
                        if (anyConfiguration)
                        {
                            WriteLine(string.Empty);
                        }
                        else
                        {
                            anyConfiguration = true;
                        }

                        this.Write("        modelBuilder");
                        this.Write(this.ToStringHelper.ToStringWithCulture(ApplyVBFixup(code.MethodChain(navigationPropertyMultiplicityConfiguration))));
                        this.Write("\r\n");
                    }
                }
            }

            this.Write("    End Sub\r\nEnd Class\r\n");
            return(this.GenerationEnvironment.ToString());
        }