public void ShouldMapDBColumn() { var mapper = new DBColumnMapper(); var column = new Column { Name = "Age", DataType = "Int", IsNullable = true }; Assert.That(mapper.Map(column, "Age", new PascalCaseTextFormatter()), Is.EqualTo("Map(x => x.Age).Column(\"Age\");")); }
public void ShouldMapDBColumnWithProperties() { var mapper = new DBColumnMapper(); var column = new Column { Name = "Name", DataLength = 16, DataType = "varchar", IsForeignKey = false, IsNullable = false, IsPrimaryKey = false, MappedDataType = "string" }; Assert.That(mapper.Map(column, "Name", new PascalCaseTextFormatter()), Is.EqualTo("Map(x => x.Name).Column(\"Name\").Not.Nullable().Length(16);")); }
public CodeCompileUnit GetCompleteCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = appPrefs.GeneratePartialClasses; var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; newType.BaseTypes.Add(string.Format("ClassMap<{0}{1}>", appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(Table.Name))); var constructor = new CodeConstructor {Attributes = MemberAttributes.Public}; constructor.Statements.Add(new CodeSnippetStatement(TABS + "Table(\"" + Table.Name + "\");")); if (appPrefs.UseLazy) constructor.Statements.Add(new CodeSnippetStatement(TABS + "LazyLoad();")); if(UsesSequence) { var fieldName = FixPropertyWithSameClassName(Table.PrimaryKey.Columns[0].Name, Table.Name); constructor.Statements.Add(new CodeSnippetStatement(String.Format(TABS + "Id(x => x.{0}).Column(x => x.{1}).GeneratedBy.Sequence(\"{2}\")", Formatter.FormatText(fieldName), fieldName, appPrefs.Sequence))); } else if (Table.PrimaryKey !=null && Table.PrimaryKey.Type == PrimaryKeyType.PrimaryKey) { var fieldName = FixPropertyWithSameClassName(Table.PrimaryKey.Columns[0].Name, Table.Name); constructor.Statements.Add(GetIdMapCodeSnippetStatement(this.appPrefs, Table, Table.PrimaryKey.Columns[0].Name, fieldName, Table.PrimaryKey.Columns[0].DataType, Formatter)); } else if (Table.PrimaryKey != null) { constructor.Statements.Add(GetIdMapCodeSnippetStatement(Table.PrimaryKey, Table, Formatter)); } // Many To One Mapping foreach (var fk in Table.ForeignKeys.Where(fk => fk.Columns.First().IsForeignKey && appPrefs.IncludeForeignKeys)) { var propertyName = appPrefs.NameFkAsForeignTable ? fk.UniquePropertyName : fk.Columns.First().Name; string name = propertyName; propertyName = Formatter.FormatSingular(propertyName); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); var pkAlsoFkQty = (from fks in Table.ForeignKeys.Where(fks => fks.UniquePropertyName == name) select fks).Count(); if (pkAlsoFkQty > 1) { constructor.Statements.Add(new CodeSnippetStatement(string.Format(TABS + "References(x => x.{0}).Column(\"{1}\").ForeignKey(\"{2}\");", fieldName, fk.Columns.First().Name, fk.Columns.First().ConstraintName))); } else { constructor.Statements.Add(new CodeSnippetStatement(string.Format(TABS + "References(x => x.{0}).Column(\"{1}\");", fieldName, fk.Columns.First().Name))); } } // Property Map foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var propertyName = Formatter.FormatText(column.Name); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); var columnMapping = new DBColumnMapper().Map(column, fieldName, Formatter, appPrefs.IncludeLengthAndScale); constructor.Statements.Add(new CodeSnippetStatement(TABS + columnMapping)); } // Bag (HasMany in FluentMapping) if (appPrefs.IncludeHasMany) Table.HasManyRelationships.ToList().ForEach(x => constructor.Statements.Add(new OneToMany(Formatter).Create(x))); newType.Members.Add(constructor); return compileUnit; }
public CodeCompileUnit GetCompleteCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = appPrefs.GeneratePartialClasses; var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; newType.BaseTypes.Add(string.Format("ClassMap<{0}{1}>", appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(Table.Name))); var constructor = new CodeConstructor {Attributes = MemberAttributes.Public}; constructor.Statements.Add(new CodeSnippetStatement(TABS + "Table(\"" + Table.Name + "\");")); constructor.Statements.Add(new CodeSnippetStatement(TABS + "LazyLoad();")); if(UsesSequence) { constructor.Statements.Add(new CodeSnippetStatement(String.Format(TABS + "Id(x => x.{0}).Column(x => x.{1}).GeneratedBy.Sequence(\"{2}\")", Formatter.FormatText(Table.PrimaryKey.Columns[0].Name), Table.PrimaryKey.Columns[0].Name, appPrefs.Sequence))); } else if (Table.PrimaryKey.Type == PrimaryKeyType.PrimaryKey) { constructor.Statements.Add(GetIdMapCodeSnippetStatement(this.appPrefs, Table.PrimaryKey.Columns[0].Name, Table.PrimaryKey.Columns[0].DataType, Formatter)); } else { constructor.Statements.Add(GetIdMapCodeSnippetStatement(Table.PrimaryKey, Formatter)); } foreach (var fk in Table.ForeignKeys.Where(fk => fk.Columns.First().IsForeignKey && appPrefs.IncludeForeignKeys)) { constructor.Statements.Add(new CodeSnippetStatement(string.Format(TABS + "References(x => x.{0}).Column(\"{1}\");", Formatter.FormatSingular(fk.UniquePropertyName), fk.Columns.First().Name))); } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var columnMapping = new DBColumnMapper().Map(column, Formatter, appPrefs.IncludeLengthAndScale); constructor.Statements.Add(new CodeSnippetStatement(TABS + columnMapping)); } Table.HasManyRelationships.ToList().ForEach(x => constructor.Statements.Add(new OneToMany(Formatter).Create(x)) ); newType.Members.Add(constructor); return compileUnit; }
public CodeCompileUnit GetCompleteCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = appPrefs.GeneratePartialClasses; var pascalCaseTextFormatter = new PascalCaseTextFormatter {PrefixRemovalList = appPrefs.FieldPrefixRemovalList}; newType.BaseTypes.Add(string.Format("EntityTypeConfiguration<{0}{1}>", appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(Table.Name))); var constructor = new CodeConstructor {Attributes = MemberAttributes.Public}; constructor.Statements.Add(new CodeSnippetStatement(TABS + "ToTable(\"" + Table.Name + "\");")); if (appPrefs.UseLazy) constructor.Statements.Add(new CodeSnippetStatement(TABS + "LazyLoad();")); if (UsesSequence) { var fieldName = FixPropertyWithSameClassName(Table.PrimaryKey.Columns[0].Name, Table.Name); constructor.Statements.Add(new CodeSnippetStatement(String.Format(TABS + "Id(x => x.{0}).Column(x => x.{1}).GeneratedBy.Sequence(\"{2}\")", Formatter.FormatText(fieldName), fieldName, appPrefs.Sequence))); } else if (Table.PrimaryKey != null && Table.PrimaryKey.Type == PrimaryKeyType.PrimaryKey) { var fieldName = FixPropertyWithSameClassName(Table.PrimaryKey.Columns[0].Name, Table.Name); constructor.Statements.Add(GetIdMapCodeSnippetStatement(appPrefs, Table, Table.PrimaryKey.Columns[0].Name, fieldName, Table.PrimaryKey.Columns[0].DataType, Formatter)); } else if (Table.PrimaryKey != null) { constructor.Statements.Add(GetIdMapCodeSnippetStatement(Table.PrimaryKey, Table, Formatter)); } // Many To One Mapping foreach (var fk in Table.ForeignKeys.Where(fk => fk.Columns.First().IsForeignKey && appPrefs.IncludeForeignKeys)) { var propertyName = appPrefs.NameFkAsForeignTable ? fk.UniquePropertyName : fk.Columns.First().Name; propertyName = Formatter.FormatSingular(propertyName); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); var propertyMapType = "HasRequired"; if (fk.IsNullable) { propertyMapType = "HasOptional"; } var codeSnippet = string.Format(TABS + "{0}(x => x.{1}).WithMany(t => t.{2}).HasForeignKey(d => d.{3});", propertyMapType, fieldName, fk.Columns.First().ForeignKeyTableName, fk.Columns.First().ForeignKeyColumnName); constructor.Statements.Add(new CodeSnippetStatement(codeSnippet)); } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var propertyName = Formatter.FormatText(column.Name); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); var columnMapping = new DBColumnMapper().Map(column, fieldName, Formatter, appPrefs.IncludeLengthAndScale); constructor.Statements.Add(new CodeSnippetStatement(TABS + columnMapping)); } if (appPrefs.IncludeHasMany) { Table.HasManyRelationships.ToList().ForEach(x => constructor.Statements.Add(new EFOneToMany(Formatter, pascalCaseTextFormatter).Create(x))); } newType.Members.Add(constructor); return compileUnit; }
public void ShouldMapDBColumnWithProperties() { var mapper = new DBColumnMapper(); var columnDetail = new ColumnDetail("Name", "varchar", 16, 0, 0, false); Assert.That(mapper.Map(columnDetail), Is.EqualTo("Map(x => x.Name).Length(16).Not.Nullable();")); }
public void ShouldMapDBColumn() { var mapper = new DBColumnMapper(); var columnDetail = new ColumnDetail("Age", "Int", 0, 0, 0, true); Assert.That(mapper.Map(columnDetail), Is.EqualTo("Map(x => x.Age);")); }