public void Type_returns_container_name() { var container = Model.ConceptualModel.Container; var code = new CSharpCodeHelper(); Assert.Equal("CodeFirstContainer", code.Type(container)); }
public void Type_returns_type_name() { var container = Model.ConceptualModel.EntityTypes.First(); var code = new CSharpCodeHelper(); Assert.Equal("Entity", code.Type(container)); }
public void Type_evaluates_preconditions() { ArgumentException ex; var code = new CSharpCodeHelper(); ex = Assert.Throws <ArgumentNullException>(() => code.Type((EntityContainer)null)); Assert.Equal("container", ex.ParamName); ex = Assert.Throws <ArgumentNullException>(() => code.Type((EdmType)null)); Assert.Equal("edmType", ex.ParamName); ex = Assert.Throws <ArgumentNullException>(() => code.Type((EdmProperty)null)); Assert.Equal("property", ex.ParamName); ex = Assert.Throws <ArgumentNullException>(() => code.Type((NavigationProperty)null)); Assert.Equal("navigationProperty", ex.ParamName); }
public void Type_returns_collection_property_type() { var property = Model.ConceptualModel.EntityTypes.First().NavigationProperties.First( p => p.Name == "Children"); var code = new CSharpCodeHelper(); Assert.Equal("ICollection<Entity>", code.Type(property)); }
public void Type_returns_reference_property_type() { var property = Model.ConceptualModel.EntityTypes.First().NavigationProperties.First( p => p.Name == "Parent"); var code = new CSharpCodeHelper(); Assert.Equal("Entity", code.Type(property)); }
public void Type_unqualifies_property_type() { var property = Model.ConceptualModel.EntityTypes.First().Properties.First( p => p.Name == "Guid"); var code = new CSharpCodeHelper(); Assert.Equal("Guid", code.Type(property)); }
public void Type_escapes_container_name() { var container = new Mock<EntityContainer>(); container.SetupGet(c => c.Name).Returns("null"); var code = new CSharpCodeHelper(); Assert.Equal("_null", code.Type(container.Object)); }
public void Type_returns_property_type() { var property = Model.ConceptualModel.EntityTypes.First().Properties.First( p => p.Name == "Name"); var code = new CSharpCodeHelper(); Assert.Equal("string", code.Type(property)); }
public void Type_evaluates_preconditions() { ArgumentException ex; var code = new CSharpCodeHelper(); ex = Assert.Throws<ArgumentNullException>(() => code.Type((EntityContainer)null)); Assert.Equal("container", ex.ParamName); ex = Assert.Throws<ArgumentNullException>(() => code.Type((EdmType)null)); Assert.Equal("edmType", ex.ParamName); ex = Assert.Throws<ArgumentNullException>(() => code.Type((EdmProperty)null)); Assert.Equal("property", ex.ParamName); ex = Assert.Throws<ArgumentNullException>(() => code.Type((NavigationProperty)null)); Assert.Equal("navigationProperty", ex.ParamName); }
public void Type_returns_nullable_value_property_type() { var property = Model.ConceptualModel.EntityTypes.First().Properties.First( p => p.Name == "ParentId"); var code = new CSharpCodeHelper(); Assert.Equal("int?", code.Type(property)); }
public void Type_escapes_type_name() { var type = new Mock <EdmType>(); type.SetupGet(c => c.Name).Returns("null"); var code = new CSharpCodeHelper(); Assert.Equal("_null", code.Type(type.Object)); }
public void Type_escapes_container_name() { var container = new Mock <EntityContainer>(); container.SetupGet(c => c.Name).Returns("null"); var code = new CSharpCodeHelper(); Assert.Equal("_null", code.Type(container.Object)); }
/// <summary> /// Create the template output /// </summary> public virtual string TransformText() { var code = new CSharpCodeHelper(); 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("namespace "); this.Write(this.ToStringHelper.ToStringWithCulture(Namespace)); this.Write("\r\n{\r\n using System;\r\n using System.Data.Entity;\r\n using System.Component" + "Model.DataAnnotations.Schema;\r\n using System.Linq;\r\n\r\n public partial clas" + "s "); this.Write(this.ToStringHelper.ToStringWithCulture(ContextClassName)); this.Write(" : DbContext\r\n {\r\n public "); this.Write(this.ToStringHelper.ToStringWithCulture(ContextClassName)); this.Write("()\r\n : base(\"name="); this.Write(this.ToStringHelper.ToStringWithCulture(ConnectionStringName)); this.Write("\")\r\n {\r\n }\r\n\r\n"); foreach (var entitySet in Model.ConceptualModel.Container.EntitySets) { this.Write(" public virtual DbSet<"); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType))); this.Write("> "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(entitySet))); this.Write(" { get; set; }\r\n"); } this.Write("\r\n protected override void OnModelCreating(DbModelBuilder modelBuilder)\r\n " + " {\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<"); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType))); this.Write(">()\r\n"); } else { WriteLine(string.Empty); } Write(" " + code.MethodChain(typeConfiguration)); } if (!firstTypeConfiguration) { WriteLine(";"); } 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<"); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType))); this.Write(">()\r\n .Property(e => e."); this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(property))); this.Write(")\r\n"); } else { WriteLine(string.Empty); } Write(" " + code.MethodChain(propertyConfiguration)); } if (!firstPropertyConfiguration) { WriteLine(";"); } } 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(code.MethodChain(navigationPropertyMultiplicityConfiguration))); this.Write("\r\n"); } else { WriteLine(string.Empty); } Write(" " + code.MethodChain(navigationPropertyConfiguration)); } if (!firstNavigationPropertyConfiguration) { WriteLine(";"); } else if (!isDefaultMultiplicity) { if (anyConfiguration) { WriteLine(string.Empty); } else { anyConfiguration = true; } this.Write(" modelBuilder"); this.Write(this.ToStringHelper.ToStringWithCulture(code.MethodChain(navigationPropertyMultiplicityConfiguration))); this.Write(";\r\n"); } } } this.Write(" }\r\n }\r\n}\r\n"); return(this.GenerationEnvironment.ToString()); }
public void Type_escapes_type_name() { var type = new Mock<EdmType>(); type.SetupGet(c => c.Name).Returns("null"); var code = new CSharpCodeHelper(); Assert.Equal("_null", code.Type(type.Object)); }
/// <summary> /// Create the template output /// </summary> public virtual string TransformText() { var code = new CSharpCodeHelper(); 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("namespace "); this.Write(this.ToStringHelper.ToStringWithCulture(Namespace)); this.Write("\r\n{\r\n using System;\r\n using System.Collections.Generic;\r\n using System.C" + "omponentModel.DataAnnotations;\r\n using System.ComponentModel.DataAnnotations." + "Schema;\r\n using System.Data.Entity.Spatial;\r\n\r\n"); var typeConfigurations = edm.GetConfigurations(EntitySet, Model).OfType<IAttributeConfiguration>(); foreach (var typeConfiguration in typeConfigurations) { this.Write(" "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Attribute(typeConfiguration))); this.Write("\r\n"); } this.Write(" public partial class "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entityType))); this.Write("\r\n {\r\n"); var collectionProperties = from p in entityType.NavigationProperties where p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many select p; if (collectionProperties.Any()) { this.Write(" [System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Usage\", \"CA22" + "14:DoNotCallOverridableMethodsInConstructors\")]\r\n public "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entityType))); this.Write("()\r\n {\r\n"); foreach (var collectionProperty in collectionProperties) { this.Write(" "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(collectionProperty))); this.Write(" = new HashSet<"); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(collectionProperty.ToEndMember.GetEntityType()))); this.Write(">();\r\n"); } this.Write(" }\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 "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(property))); this.Write(" "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(property))); this.Write(" { get; set; }\r\n"); } foreach (var navigationProperty in entityType.NavigationProperties) { if (!first) { WriteLine(string.Empty); } else { first = false; } if (navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many) { this.Write(" [System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Usage\", \"CA22" + "27:CollectionPropertiesShouldBeReadOnly\")]\r\n"); } this.Write(" public virtual "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(navigationProperty))); this.Write(" "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(navigationProperty))); this.Write(" { get; set; }\r\n"); } this.Write(" }\r\n}\r\n"); return this.GenerationEnvironment.ToString(); }
/// <summary> /// Create the template output /// </summary> public virtual string TransformText() { var code = new CSharpCodeHelper(); 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("namespace "); this.Write(this.ToStringHelper.ToStringWithCulture(Namespace)); this.Write("\r\n{\r\n using System;\r\n using System.Collections.Generic;\r\n using System.C" + "omponentModel.DataAnnotations;\r\n using System.ComponentModel.DataAnnotations." + "Schema;\r\n using System.Data.Entity.Spatial;\r\n\r\n"); var typeConfigurations = edm.GetConfigurations(EntitySet, Model).OfType <IAttributeConfiguration>(); foreach (var typeConfiguration in typeConfigurations) { this.Write(" "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Attribute(typeConfiguration))); this.Write("\r\n"); } this.Write(" public partial class "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entityType))); this.Write("\r\n {\r\n"); var collectionProperties = from p in entityType.NavigationProperties where p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many select p; if (collectionProperties.Any()) { this.Write(" public "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entityType))); this.Write("()\r\n {\r\n"); foreach (var collectionProperty in collectionProperties) { this.Write(" "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(collectionProperty))); this.Write(" = new HashSet<"); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(collectionProperty.ToEndMember.GetEntityType()))); this.Write(">();\r\n"); } this.Write(" }\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 "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(property))); this.Write(" "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(property))); this.Write(" { get; set; }\r\n"); } foreach (var navigationProperty in entityType.NavigationProperties) { if (!first) { WriteLine(string.Empty); } else { first = false; } this.Write(" public virtual "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(navigationProperty))); this.Write(" "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(navigationProperty))); this.Write(" { get; set; }\r\n"); } this.Write(" }\r\n}\r\n"); return(this.GenerationEnvironment.ToString()); }
/// <summary> /// Create the template output /// </summary> public virtual string TransformText() { var code = new CSharpCodeHelper(); 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("namespace "); this.Write(this.ToStringHelper.ToStringWithCulture(Namespace)); this.Write("\r\n{\r\n using System;\r\n using System.Data.Entity;\r\n using System.Component" + "Model.DataAnnotations.Schema;\r\n using System.Linq;\r\n\r\n public partial clas" + "s "); this.Write(this.ToStringHelper.ToStringWithCulture(ContextClassName)); this.Write(" : DbContext\r\n {\r\n public "); this.Write(this.ToStringHelper.ToStringWithCulture(ContextClassName)); this.Write("()\r\n : base(\"name="); this.Write(this.ToStringHelper.ToStringWithCulture(ConnectionStringName)); this.Write("\")\r\n {\r\n }\r\n\r\n"); foreach (var entitySet in Model.ConceptualModel.Container.EntitySets) { this.Write(" public virtual DbSet<"); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType))); this.Write("> "); this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(entitySet))); this.Write(" { get; set; }\r\n"); } this.Write("\r\n protected override void OnModelCreating(DbModelBuilder modelBuilder)\r\n " + " {\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<"); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType))); this.Write(">()\r\n"); } else { WriteLine(string.Empty); } Write(" " + code.MethodChain(typeConfiguration)); } if (!firstTypeConfiguration) { WriteLine(";"); } 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<"); this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entitySet.ElementType))); this.Write(">()\r\n .Property(e => e."); this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(property))); this.Write(")\r\n"); } else { WriteLine(string.Empty); } Write(" " + code.MethodChain(propertyConfiguration)); } if (!firstPropertyConfiguration) { WriteLine(";"); } } 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(code.MethodChain(navigationPropertyMultiplicityConfiguration))); this.Write("\r\n"); } else { WriteLine(string.Empty); } Write(" " + code.MethodChain(navigationPropertyConfiguration)); } if (!firstNavigationPropertyConfiguration) { WriteLine(";"); } else if (!isDefaultMultiplicity) { if (anyConfiguration) { WriteLine(string.Empty); } else { anyConfiguration = true; } this.Write(" modelBuilder"); this.Write(this.ToStringHelper.ToStringWithCulture(code.MethodChain(navigationPropertyMultiplicityConfiguration))); this.Write(";\r\n"); } } } this.Write(" }\r\n }\r\n}\r\n"); return this.GenerationEnvironment.ToString(); }