public CodeCompileUnit GetCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnitWithInheritanceAndInterface(nameSpace, className, appPrefs.InheritenceAndInterfaces); var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = appPrefs.GeneratePartialClasses; CreateProperties(codeGenerationHelper, mapper, newType); var constructorStatements = new CodeStatementCollection(); foreach (var hasMany in Table.HasManyRelationships) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(appPrefs.ForeignEntityCollectionType + "<" + appPrefs.ClassNamePrefix + Formatter.FormatSingular(hasMany.Reference) + ">", "Lista" + Formatter.FormatSingular(hasMany.Reference), appPrefs.UseLazy)); //constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = new {1}<{2}{3}>();", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, Formatter.FormatSingular(hasMany.Reference)))); } var constructor = new CodeConstructor { Attributes = MemberAttributes.Public }; constructor.Statements.AddRange(constructorStatements); newType.Members.Add(constructor); return(compileUnit); }
private void CreateFields(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { if (Table.PrimaryKey != null) { foreach (var pk in Table.PrimaryKey.Columns) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, Formatter.FormatText(pk.Name), true)); } } if (appPrefs.IncludeForeignKeys) { var pascalCaseTextFormatter = new PascalCaseTextFormatter(); // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. foreach (var fk in Table.ForeignKeys.Where(fk => !string.IsNullOrEmpty(fk.References))) { var fieldName = Formatter.FormatSingular(fk.UniquePropertyName); var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.References); newType.Members.Add(codeGenerationHelper.CreateField(typeName, fieldName)); } } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); var fieldName = Formatter.FormatText(column.Name); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, fieldName, column.IsNullable)); } }
private void CreateFullProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { foreach (var pk in Table.PrimaryKey.Columns) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, Formatter.FormatText(pk.Name), true)); newType.Members.Add(codeGenerationHelper.CreateProperty(mapFromDbType, Formatter.FormatText(pk.Name), appPrefs.UseLazy)); } // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. foreach (var fk in Table.ForeignKeys.Where(fk => !string.IsNullOrEmpty(fk.References))) { newType.Members.Add(codeGenerationHelper.CreateField(appPrefs.ClassNamePrefix + Formatter.FormatSingular(fk.References), Formatter.FormatSingular(fk.UniquePropertyName))); newType.Members.Add(codeGenerationHelper.CreateProperty(appPrefs.ClassNamePrefix + Formatter.FormatSingular(fk.References), Formatter.FormatSingular(fk.UniquePropertyName), appPrefs.UseLazy)); } foreach (var column in Table.Columns.Where(x => x.IsPrimaryKey != true)) { if (!appPrefs.IncludeForeignKeys && column.IsForeignKey) { continue; } var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, Formatter.FormatText(column.Name), true, column.IsNullable)); newType.Members.Add(codeGenerationHelper.CreateProperty(mapFromDbType, Formatter.FormatText(column.Name), column.IsNullable, appPrefs.UseLazy)); } }
public CodeCompileUnit GetCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnitWithInheritanceAndInterface(nameSpace, className, appPrefs.InheritenceAndInterfaces); var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = appPrefs.GeneratePartialClasses; CreateProperties(codeGenerationHelper, mapper, newType); // Generate GetHashCode() and Equals() methods. if (Table.PrimaryKey != null && Table.PrimaryKey.Columns.Count != 0 && Table.PrimaryKey.Type == PrimaryKeyType.CompositeKey) { var pkColsList = Table.PrimaryKey.Columns.Select(s => Formatter.FormatText(s.Name)).ToList(); var equalsCode = CreateCompositeKeyEqualsMethod(pkColsList); var getHashKeyCode = CreateCompositeKeyGetHashKeyMethod(pkColsList); equalsCode.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "NHibernate Composite Key Requirements")); newType.Members.Add(equalsCode); newType.Members.Add(getHashKeyCode); getHashKeyCode.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty)); } // Dont create a constructor if there are no relationships. if (Table.HasManyRelationships.Count == 0) { return(compileUnit); } var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; var constructorStatements = new CodeStatementCollection(); foreach (var hasMany in Table.HasManyRelationships) { if (appPrefs.Language == Language.CSharp) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(string.Format("{0}<{1}{2}>", appPrefs.ForeignEntityCollectionType, appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)), Formatter.FormatPlural(hasMany.Reference), appPrefs.UseLazy)); constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = new {1}<{2}{3}>();", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)))); } else if (appPrefs.Language == Language.VB) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(string.Format("{0}(Of {1}{2})", appPrefs.ForeignEntityCollectionType, appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)), Formatter.FormatPlural(hasMany.Reference), appPrefs.UseLazy)); constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = New {1}(Of {2}{3})()", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)))); } } var constructor = new CodeConstructor { Attributes = MemberAttributes.Public }; constructor.Statements.AddRange(constructorStatements); newType.Members.Add(constructor); return(compileUnit); }
private void CreateAutoProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { if (Table.PrimaryKey != null) { foreach (var pk in Table.PrimaryKey.Columns) { if (pk.IsForeignKey && appPrefs.IncludeForeignKeys) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(Formatter.FormatSingular(pk.ForeignKeyTableName), Formatter.FormatSingular(pk.ForeignKeyTableName), appPrefs.UseLazy)); } else { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); var fieldName = FixPropertyWithSameClassName(pk.Name, Table.Name); var pkAlsoFkQty = (from fk in Table.ForeignKeys.Where(fk => fk.UniquePropertyName == pk.Name) select fk).Count(); if (pkAlsoFkQty > 0) { fieldName = fieldName + "Id"; } newType.Members.Add(codeGenerationHelper.CreateAutoProperty(mapFromDbType.ToString(), Formatter.FormatText(fieldName), appPrefs.UseLazy)); } } } if (appPrefs.IncludeForeignKeys) { var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. string lastOne = null; foreach (var fk in Table.Columns.Where(c => c.IsForeignKey && !c.IsPrimaryKey)) { var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.ForeignKeyTableName); var propertyName = Formatter.FormatSingular(fk.ForeignKeyTableName); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); if (lastOne != fieldName) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(typeName, fieldName, appPrefs.UseLazy)); } lastOne = fieldName; } } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); var fieldName = FixPropertyWithSameClassName(column.Name, Table.Name); var property = codeGenerationHelper.CreateAutoProperty(mapFromDbType, Formatter.FormatText(fieldName), column.IsNullable, appPrefs.UseLazy); AttachValidatorAttributes(ref property, column); newType.Members.Add(property); } }
public CodeCompileUnit GetCompleteCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className); var newType = compileUnit.Namespaces[0].Types[0]; newType.BaseTypes.Add(string.Format("ClassMapping<{0}{1}>", appPrefs.ClassNamePrefix, Formatter.FormatSingular(Table.Name))); var constructor = new CodeConstructor { Attributes = MemberAttributes.Public }; newType.Members.Add(constructor); constructor.Statements.Add(new CodeSnippetStatement(TABS + "Table(\"" + tableName + "\");")); constructor.Statements.Add(GetIdMapCodeSnippetStatement()); 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(appPrefs, Table.PrimaryKey.Columns[0].Name, Table.PrimaryKey.Columns[0].DataType, Formatter)); } foreach (var fk in Table.ForeignKeys.Where(fk => !string.IsNullOrEmpty(fk.References))) { var referencesSnippet = string.Format("References(x => x.{0})", Formatter.FormatSingular(fk.UniquePropertyName)); var columnsSnippet = fk.AllColumnsNamesForTheSameConstraint.Length == 1 ? string.Format(".Column(\"{0}\");", fk.Name) : string.Format(".Columns({0});", fk.AllColumnsNamesForTheSameConstraint.Aggregate("new string[] { ", (a, b) => a + "\"" + b + "\", ", c => c.Substring(0, c.Length - 2) + " }")); constructor.Statements.Add(new CodeSnippetStatement(string.Format(TABS + "{0}{1}", referencesSnippet, columnsSnippet))); } foreach (var columnDetail in Table.Columns) { if (columnDetail.IsPrimaryKey) { continue; } if (columnDetail.IsForeignKey) { var manyToOneMapping = TABS + "ManyToOne(x => x." + columnDetail.Name + ", map => {map.Column(\"" + columnDetail.Name + "\"); map.NotNullable(" + (!columnDetail.IsNullable).ToString().ToLower() + "); map.Cascade(Cascade.None); });"; constructor.Statements.Add(new CodeSnippetStatement(manyToOneMapping)); } else { var columnMapping = MapNhStyle(columnDetail); constructor.Statements.Add(new CodeSnippetStatement(TABS + columnMapping)); } } return(compileUnit); }
public void ShouldGenerateField() { var codeGenerationHelper = new CodeGenerationHelper(); CodeMemberField codeMemberField = codeGenerationHelper.CreateField(typeof (string), "name"); CodeCompileUnit codeCompileUnit = codeGenerationHelper.GetCodeCompileUnit("someNamespace", "someType"); codeCompileUnit.Namespaces[0].Types[0].Members.Add(codeMemberField); cSharpCodeProvider.GenerateCodeFromCompileUnit(codeCompileUnit, new StringWriter(stringBuilder), new CodeGeneratorOptions()); Assert.IsTrue(stringBuilder.ToString().Contains("private string name;")); }
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 GetCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnitWithInheritanceAndInterface(nameSpace, className, appPrefs.InheritenceAndInterfaces); var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = appPrefs.GeneratePartialClasses; CreateProperties(codeGenerationHelper, mapper, newType); // Generate GetHashCode() and Equals() methods. if (Table.PrimaryKey != null && Table.PrimaryKey.Columns.Count != 0 && Table.PrimaryKey.Type == PrimaryKeyType.CompositeKey) { var pkColsList = Table.PrimaryKey.Columns.Select(s => Formatter.FormatText(s.Name)).ToList(); var equalsCode = CreateCompositeKeyEqualsMethod(pkColsList); var getHashKeyCode = CreateCompositeKeyGetHashKeyMethod(pkColsList); equalsCode.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "NHibernate Composite Key Requirements")); newType.Members.Add(equalsCode); newType.Members.Add(getHashKeyCode); getHashKeyCode.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty)); } // Dont create a constructor if there are no relationships. if (Table.HasManyRelationships.Count == 0) return compileUnit; var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; var constructorStatements = new CodeStatementCollection(); foreach (var hasMany in Table.HasManyRelationships) { if (appPrefs.Language == Language.CSharp) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(string.Format("{0}<{1}{2}>", appPrefs.ForeignEntityCollectionType, appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)), Formatter.FormatPlural(hasMany.Reference), appPrefs.UseLazy)); constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = new {1}<{2}{3}>();", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)))); } else if (appPrefs.Language == Language.VB) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(string.Format("{0}(Of {1}{2})", appPrefs.ForeignEntityCollectionType, appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)), Formatter.FormatPlural(hasMany.Reference), appPrefs.UseLazy)); constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = New {1}(Of {2}{3})()", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)))); } } var constructor = new CodeConstructor { Attributes = MemberAttributes.Public }; constructor.Statements.AddRange(constructorStatements); newType.Members.Add(constructor); return compileUnit; }
public CodeCompileUnit GetCompileUnit() { var codeGenerationHelper = new CodeGenerationHelper(); // This is where we construct the constructor var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, Table.Name.GetFormattedText().MakeSingular(), true); var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = applicationPreferences.GeneratePartialClasses; if (Table.PrimaryKey != null && Table.PrimaryKey.Columns.Count() != 0) { foreach (var pk in Table.PrimaryKey.Columns) { var mapFromDbType = mapper.MapFromDBType(this.applicationPreferences.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); var declaration = new CodeAttributeDeclaration("PrimaryKey"); declaration.Arguments.Add(new CodeAttributeArgument("Column", new CodePrimitiveExpression(pk.Name))); newType.Members.Add(codeGenerationHelper.CreateAutoProperty(mapFromDbType.ToString(), pk.Name.GetFormattedText(), declaration)); } } foreach (var fk in Table.ForeignKeys) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(fk.References.GetFormattedText().MakeSingular(), fk.References.GetFormattedText().MakeSingular())); } foreach (var property in Table.Columns.Where(x => x.IsPrimaryKey != true && x.IsForeignKey != true)) { var declaration = new CodeAttributeDeclaration("Property"); declaration.Arguments.Add(new CodeAttributeArgument("Column", new CodePrimitiveExpression(property.Name))); if (property.DataLength.HasValue) { declaration.Arguments.Add(new CodeAttributeArgument("Length", new CodePrimitiveExpression(property.DataLength))); } if (!property.IsNullable) { declaration.Arguments.Add(new CodeAttributeArgument("NotNull", new CodePrimitiveExpression(true))); } var mapFromDbType = mapper.MapFromDBType(this.applicationPreferences.ServerType, property.DataType, property.DataLength, property.DataPrecision, property.DataScale); newType.Members.Add(codeGenerationHelper.CreateAutoProperty(mapFromDbType.ToString(), property.Name.GetFormattedText(), declaration)); } 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; newType.BaseTypes.Add(string.Format("ClassMapping<{0}{1}>", appPrefs.ClassNamePrefix, Formatter.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 + string.Format("Lazy({0});", appPrefs.UseLazy ? "true" : "false"))); var mapper = new DBColumnMapper(); if (UsesSequence) { constructor.Statements.Add(new CodeSnippetStatement(TABS + mapper.IdSequenceMap(Table.PrimaryKey.Columns[0], appPrefs.Sequence, Formatter))); } else if (Table.PrimaryKey.Type == PrimaryKeyType.PrimaryKey) { constructor.Statements.Add(new CodeSnippetStatement(TABS + mapper.IdMap(Table.PrimaryKey.Columns[0], Formatter))); } else { } foreach (var column in Table.Columns.Where(x => x.IsPrimaryKey != true)) { if (!appPrefs.IncludeForeignKeys && column.IsForeignKey) { continue; } constructor.Statements.Add(new CodeSnippetStatement(TABS + mapper.Map(column, Formatter))); } foreach (var fk in Table.ForeignKeys.Where(fk => !string.IsNullOrEmpty(fk.References))) { constructor.Statements.Add(new CodeSnippetStatement(mapper.Reference(fk, Formatter))); } Table.HasManyRelationships.ToList().ForEach(x => constructor.Statements.Add(new CodeSnippetStatement(mapper.Bag(x, Formatter)))); newType.Members.Add(constructor); return(compileUnit); }
public CodeCompileUnit GetCompileUnit() { var codeGenerationHelper = new CodeGenerationHelper(); // This is where we construct the constructor var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, Table.Name.GetFormattedText().MakeSingular(),true); var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = applicationPreferences.GeneratePartialClasses; if (Table.PrimaryKey != null && Table.PrimaryKey.Columns.Count() != 0) { foreach (var pk in Table.PrimaryKey.Columns) { var mapFromDbType = mapper.MapFromDBType(this.applicationPreferences.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); var declaration = new CodeAttributeDeclaration("PrimaryKey"); declaration.Arguments.Add(new CodeAttributeArgument("Column", new CodePrimitiveExpression(pk.Name))); newType.Members.Add(codeGenerationHelper.CreateAutoProperty(mapFromDbType.ToString(), pk.Name.GetFormattedText(), declaration)); } } foreach (var fk in Table.ForeignKeys) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(fk.References.GetFormattedText().MakeSingular(), fk.References.GetFormattedText().MakeSingular())); } foreach (var property in Table.Columns.Where(x => x.IsPrimaryKey != true && x.IsForeignKey != true)) { var declaration = new CodeAttributeDeclaration("Property"); declaration.Arguments.Add(new CodeAttributeArgument("Column", new CodePrimitiveExpression(property.Name))); if(property.DataLength.HasValue) declaration.Arguments.Add(new CodeAttributeArgument("Length", new CodePrimitiveExpression(property.DataLength))); if (!property.IsNullable) { declaration.Arguments.Add(new CodeAttributeArgument("NotNull", new CodePrimitiveExpression(true))); } var mapFromDbType = mapper.MapFromDBType(this.applicationPreferences.ServerType, property.DataType, property.DataLength, property.DataPrecision, property.DataScale); newType.Members.Add(codeGenerationHelper.CreateAutoProperty(mapFromDbType.ToString(), property.Name.GetFormattedText(), declaration)); } 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 GetCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className); var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; var nameArgument = new CodeAttributeArgument("Name", new CodeSnippetExpression("\"" + className + "\" ")); var nameSpaceArgument = new CodeAttributeArgument("Namespace", new CodeSnippetExpression("\"\"")); newType.CustomAttributes = new CodeAttributeDeclarationCollection { new CodeAttributeDeclaration("DataContract", nameArgument, nameSpaceArgument) }; foreach (var column in table.Columns) { if (column.IsPrimaryKey) { if (appPrefs.IncludeHasMany) { foreach (var foreignKeyTable in table.HasManyRelationships) { var fkEntityName = appPrefs.ClassNamePrefix + foreignKeyTable.Reference.MakeSingular().GetPreferenceFormattedText(appPrefs); newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute("IList<" + fkEntityName + ">", foreignKeyTable.Reference.MakePlural().GetPreferenceFormattedText(appPrefs))); } } var primaryKeyType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(primaryKeyType.Name, "Id")); continue; } if (column.IsForeignKey) { var fKey = column.ForeignKeyTableName; var typeName = appPrefs.ClassNamePrefix + fKey.MakeSingular().GetPreferenceFormattedText(appPrefs); var codeMemberProperty = codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(typeName, fKey.MakeSingular().GetPreferenceFormattedText(appPrefs)); newType.Members.Add(codeMemberProperty); continue; } var propertyName = column.Name.GetPreferenceFormattedText(appPrefs); var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(mapFromDbType.Name, propertyName)); } return(compileUnit); }
public void ShouldGenerateAutoProperty() { var codeGenerationHelper = new CodeGenerationHelper(); CodeMemberProperty autoProperty = codeGenerationHelper.CreateAutoProperty(typeof (string), "Name", false); CodeCompileUnit codeCompileUnit = codeGenerationHelper.GetCodeCompileUnit("someNamespace", "someType"); codeCompileUnit.Namespaces[0].Types[0].Members.Add(autoProperty); cSharpCodeProvider.GenerateCodeFromCompileUnit(codeCompileUnit, new StringWriter(stringBuilder), new CodeGeneratorOptions()); var builderCompare = new StringBuilder(); builderCompare.AppendLine("public virtual string Name {"); builderCompare.AppendLine(" get {"); builderCompare.AppendLine(" }"); builderCompare.AppendLine(" set {"); builderCompare.AppendLine(" }"); builderCompare.Append(" }"); Assert.IsTrue(stringBuilder.ToString().Contains(builderCompare.ToString())); }
private void CreateProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { switch (appPrefs.FieldGenerationConvention) { case FieldGenerationConvention.Field: CreateFields(codeGenerationHelper, mapper, newType); break; case FieldGenerationConvention.Property: CreateFullProperties(codeGenerationHelper, mapper, newType); break; case FieldGenerationConvention.AutoProperty: CreateAutoProperties(codeGenerationHelper, mapper, newType); break; } }
public CodeCompileUnit GetCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className); var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; var nameArgument = new CodeAttributeArgument("Name", new CodeSnippetExpression("\"" + className + "\" ")); var nameSpaceArgument = new CodeAttributeArgument("Namespace", new CodeSnippetExpression("\"\"")); newType.CustomAttributes = new CodeAttributeDeclarationCollection {new CodeAttributeDeclaration("DataContract", nameArgument, nameSpaceArgument)}; foreach (var column in table.Columns) { if(column.IsPrimaryKey) { if(appPrefs.IncludeHasMany) foreach (var foreignKeyTable in table.HasManyRelationships) { var fkEntityName = appPrefs.ClassNamePrefix + foreignKeyTable.Reference.MakeSingular().GetPreferenceFormattedText(appPrefs); newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute("IList<" + fkEntityName + ">", foreignKeyTable.Reference.MakePlural().GetPreferenceFormattedText(appPrefs))); } var primaryKeyType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(primaryKeyType.Name, "Id")); continue; } if (column.IsForeignKey) { var fKey = column.ForeignKeyTableName; var typeName = appPrefs.ClassNamePrefix + fKey.MakeSingular().GetPreferenceFormattedText(appPrefs); var codeMemberProperty = codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(typeName, fKey.MakeSingular().GetPreferenceFormattedText(appPrefs)); newType.Members.Add(codeMemberProperty); continue; } var propertyName = column.Name.GetPreferenceFormattedText(appPrefs); var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(mapFromDbType.Name, propertyName)); } return compileUnit; }
private void CreateFullProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { var camelCaseFormatter = new CamelCaseTextFormatter(); if (Table.PrimaryKey != null) { foreach (var pk in Table.PrimaryKey.Columns) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, "_" + camelCaseFormatter.FormatText(pk.Name), true)); newType.Members.Add(codeGenerationHelper.CreateProperty(mapFromDbType, Formatter.FormatText(pk.Name), appPrefs.UseLazy)); } } if (appPrefs.IncludeForeignKeys) { var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. foreach (var fk in Table.ForeignKeys.Where(fk => !string.IsNullOrEmpty(fk.References))) { var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.References); newType.Members.Add(codeGenerationHelper.CreateField(typeName, string.Format("_{0}", camelCaseFormatter.FormatSingular(fk.UniquePropertyName)))); newType.Members.Add(codeGenerationHelper.CreateProperty(typeName, Formatter.FormatSingular(fk.UniquePropertyName), appPrefs.UseLazy)); } } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, "_" + camelCaseFormatter.FormatText(column.Name), column.IsNullable)); var property = codeGenerationHelper.CreateProperty(mapFromDbType, Formatter.FormatText(column.Name), column.IsNullable, appPrefs.UseLazy); AttachValidatorAttributes(ref property, column); newType.Members.Add(property); } }
public override void Generate(bool writeToFile = true) { var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = applicationPreferences.FieldPrefixRemovalList }; string className = pascalCaseTextFormatter.FormatSingular(Table.Name); string testName = $"{applicationPreferences.ClassNamePrefix}{className}_Test"; var testMethod = BuildTestMethod(className); var compileUnit = new CodeGenerationHelper().GetCodeCompileUnit(nameSpace, "PersistenceTests"); var type = compileUnit.Namespaces[0].Types[0]; type.Members.Add(testMethod); var generatedCode = GenerateCode(compileUnit, testName); if (writeToFile) { WriteToFile(generatedCode, className); } }
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("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)); } var columnMapper = new DBColumnMapper(appPrefs); 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 = columnMapper.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 CodeCompileUnit GetCompleteCompileUnit(string mapName) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, mapName); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = appPrefs.GeneratePartialClasses; var className = Formatter.FormatSingular(Table.Name); switch (appPrefs.Language) { case Language.CSharp: newType.BaseTypes.Add(string.Format("ClassMapping<{0}{1}>", appPrefs.ClassNamePrefix, className)); break; case Language.VB: newType.BaseTypes.Add(string.Format("ClassMapping(Of {0}{1})", appPrefs.ClassNamePrefix, className)); break; } var constructor = new CodeConstructor { Attributes = MemberAttributes.Public }; // Table Name - Only ouput if table is different than the class name. if (Table.Name.ToLower() != className.ToLower()) { constructor.Statements.Add(new CodeSnippetStatement(TABS + "Table(\"" + Table.Name + "\");")); } // Scheme / Owner Name if (!string.IsNullOrEmpty(Table.Owner)) { constructor.Statements.Add(new CodeSnippetStatement(TABS + "Schema(\"" + Table.Owner + "\");")); } constructor.Statements.Add(new CodeSnippetStatement(TABS + string.Format("Lazy({0});", appPrefs.UseLazy ? "true" : "false"))); var mapper = new DBColumnMapper(appPrefs); // Id or ComposedId Map if (Table.PrimaryKey != null) { if (UsesSequence) { constructor.Statements.Add( new CodeSnippetStatement(TABS + mapper.IdSequenceMap(Table.PrimaryKey.Columns[0], appPrefs.Sequence, Formatter))); } else if (Table.PrimaryKey.Type == PrimaryKeyType.PrimaryKey) { constructor.Statements.Add( new CodeSnippetStatement(TABS + mapper.IdMap(Table.PrimaryKey.Columns[0], Formatter))); } else if (Table.PrimaryKey.Type == PrimaryKeyType.CompositeKey) { var pkColumns = Table.PrimaryKey.Columns; constructor.Statements.Add( new CodeSnippetStatement(TABS + mapper.CompositeIdMap(pkColumns, Formatter))); } } // Property Map foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { constructor.Statements.Add(new CodeSnippetStatement(TABS + mapper.Map(column, Formatter, appPrefs.IncludeLengthAndScale))); } // Many To One Mapping foreach (var fk in Table.ForeignKeys.Where(fk => fk.Columns.First().IsForeignKey&& appPrefs.IncludeForeignKeys)) { constructor.Statements.Add(new CodeSnippetStatement(mapper.Reference(fk, Formatter))); } // Bag Table.HasManyRelationships.ToList().ForEach(x => constructor.Statements.Add(new CodeSnippetStatement(mapper.Bag(x, Formatter)))); newType.Members.Add(constructor); // Strip out any semicolons if (appPrefs.Language == Language.VB) { foreach (CodeSnippetStatement statement in constructor.Statements) { statement.Value = statement.Value.Replace(";", string.Empty); } } return(compileUnit); }
private void CreateAutoProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { if (Table.PrimaryKey != null) { foreach (var pk in Table.PrimaryKey.Columns) { if (pk.IsForeignKey && appPrefs.IncludeForeignKeys) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(Formatter.FormatSingular(pk.ForeignKeyTableName), Formatter.FormatSingular(pk.ForeignKeyTableName), appPrefs.UseLazy)); } else { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); var fieldName = FixPropertyWithSameClassName(pk.Name, Table.Name); var pkAlsoFkQty = (from fk in Table.ForeignKeys.Where(fk => fk.UniquePropertyName == pk.Name) select fk).Count(); if (pkAlsoFkQty > 0) fieldName = fieldName + "Id"; newType.Members.Add(codeGenerationHelper.CreateAutoProperty(mapFromDbType.ToString(), Formatter.FormatText(fieldName), appPrefs.UseLazy)); } } } if (appPrefs.IncludeForeignKeys) { var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. string lastOne = null; foreach (var fk in Table.Columns.Where(c => c.IsForeignKey && !c.IsPrimaryKey)) { var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.ForeignKeyTableName); if (String.IsNullOrEmpty(typeName)) { System.Diagnostics.Trace.WriteLine(String.Format("Skipping null ForeignKeyTableName for field {0}", fk.Name)); continue; } var propertyName = Formatter.FormatSingular(fk.ForeignKeyTableName); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); if (lastOne != fieldName) newType.Members.Add(codeGenerationHelper.CreateAutoProperty(typeName, fieldName, appPrefs.UseLazy)); lastOne = fieldName; } } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); var fieldName = FixPropertyWithSameClassName(column.Name, Table.Name); var property = codeGenerationHelper.CreateAutoProperty(mapFromDbType, Formatter.FormatText(fieldName), column.IsNullable, appPrefs.UseLazy); AttachValidatorAttributes(ref property, column); newType.Members.Add(property); } }
private void CreateAutoProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { if (Table.PrimaryKey != null) { foreach (var pk in Table.PrimaryKey.Columns) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); newType.Members.Add(codeGenerationHelper.CreateAutoProperty(mapFromDbType.ToString(), Formatter.FormatText(pk.Name), appPrefs.UseLazy)); } } if (appPrefs.IncludeForeignKeys) { var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. foreach (var fk in Table.ForeignKeys.Where(fk => !string.IsNullOrEmpty(fk.References))) { var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.References); newType.Members.Add(codeGenerationHelper.CreateAutoProperty(typeName, Formatter.FormatSingular(fk.UniquePropertyName), appPrefs.UseLazy)); } } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); var property = codeGenerationHelper.CreateAutoProperty(mapFromDbType, Formatter.FormatText(column.Name), column.IsNullable, appPrefs.UseLazy); AttachValidatorAttributes(ref property, column); newType.Members.Add(property); } }
private void CreateFullProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { var camelCaseFormatter = new CamelCaseTextFormatter(); if (Table.PrimaryKey != null) { foreach (var pk in Table.PrimaryKey.Columns) { if (pk.IsForeignKey && appPrefs.IncludeForeignKeys) { newType.Members.Add(codeGenerationHelper.CreateField(Formatter.FormatSingular(pk.ForeignKeyTableName), "_" + camelCaseFormatter.FormatSingular(pk.ForeignKeyTableName))); newType.Members.Add(codeGenerationHelper.CreateProperty(Formatter.FormatSingular(pk.ForeignKeyTableName), Formatter.FormatSingular(pk.ForeignKeyTableName), appPrefs.UseLazy)); } else { var mapFromDbType = mapper.MapFromDBType(appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); var pkAlsoFkQty = (from fk in Table.ForeignKeys.Where(fk => fk.UniquePropertyName == pk.Name) select fk).Count(); var fieldName = FixPropertyWithSameClassName(pk.Name, Table.Name); if (pkAlsoFkQty > 0) { fieldName = fieldName + "Id"; } newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, "_" + camelCaseFormatter.FormatText(fieldName), true)); newType.Members.Add(codeGenerationHelper.CreateProperty(mapFromDbType, Formatter.FormatText(fieldName), appPrefs.UseLazy)); } } } if (appPrefs.IncludeForeignKeys) { var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. foreach (var fk in Table.Columns.Where(c => c.IsForeignKey && !c.IsPrimaryKey)) { var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.ForeignKeyTableName); var propertyName = fk.ForeignKeyTableName; var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); newType.Members.Add(codeGenerationHelper.CreateField(typeName, string.Format("_{0}", camelCaseFormatter.FormatSingular(fieldName)))); newType.Members.Add(codeGenerationHelper.CreateProperty(typeName, Formatter.FormatSingular(fieldName), appPrefs.UseLazy)); } } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var mapFromDbType = mapper.MapFromDBType(appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); var fieldName = FixPropertyWithSameClassName(column.Name, Table.Name); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, "_" + camelCaseFormatter.FormatText(fieldName), column.IsNullable)); var property = codeGenerationHelper.CreateProperty(mapFromDbType, Formatter.FormatText(fieldName), column.IsNullable, appPrefs.UseLazy); AttachValidatorAttributes(ref property, column); newType.Members.Add(property); } }
private void CreateFields(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { if (Table.PrimaryKey != null) { foreach (var pk in Table.PrimaryKey.Columns) { var mapFromDbType = mapper.MapFromDBType(appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); var propertyName = Formatter.FormatText(pk.Name); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); var pkAlsoFkQty = (from fk in Table.ForeignKeys.Where(fk => fk.UniquePropertyName == pk.Name) select fk).Count(); if (pkAlsoFkQty > 0) fieldName = fieldName + "Id"; newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, fieldName, true)); } } if (appPrefs.IncludeForeignKeys) { var pascalCaseTextFormatter = new PascalCaseTextFormatter(); // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. foreach (var fk in Table.ForeignKeys.Where(fk => !string.IsNullOrEmpty(fk.References))) { var propertyName = fk.UniquePropertyName; propertyName = Formatter.FormatSingular(propertyName); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.References); newType.Members.Add(codeGenerationHelper.CreateField(typeName, fieldName)); } } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var mapFromDbType = mapper.MapFromDBType(appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); var propertyName = Formatter.FormatText(column.Name); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, fieldName, column.IsNullable)); } }
public CodeCompileUnit GetCompileUnit(out CodeCompileUnit pkClass) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, tableName.GetFormattedText()); CodeCompileUnit pkCompileUnit = null; var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; List<ColumnDetail> pkColumns = columnDetails.FindAll(col => col.IsPrimaryKey); List<ColumnDetail> normalColumns = columnDetails.FindAll(col => col.IsPrimaryKey == false); // data contract count int dataContractCount = 1; // create pk columns if(pkColumns.Count > 0) { if(pkColumns.Count == 1) { var columnDetail = pkColumns[0]; string propertyName = columnDetail.ColumnName.GetPreferenceFormattedText(applicationPreferences); Type mapFromDbType = mapper.MapFromDBType(columnDetail.DataType, columnDetail.DataLength, columnDetail.DataPrecision, columnDetail.DataScale); switch (applicationPreferences.FieldGenerationConvention) { case FieldGenerationConvention.Property: newType.Members.Add(codeGenerationHelper.CreateProperty(mapFromDbType, propertyName.MakeFirstCharUpperCase())); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, propertyName.MakeFirstCharLowerCase())); break; case FieldGenerationConvention.AutoProperty: propertyName = columnDetail.ColumnName.GetFormattedText(); var codeMemberProperty = codeGenerationHelper.CreateAutoProperty(mapFromDbType, propertyName); newType.Members.Add(codeMemberProperty); break; default: newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, propertyName)); break; } } else // composite key { string pkClassName = tableName.GetFormattedText() + "PK"; string pkRefProperty = pkClassName; pkCompileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, pkClassName); var newPKType = pkCompileUnit.Namespaces[0].Types[0]; // create composite member switch (applicationPreferences.FieldGenerationConvention) { case FieldGenerationConvention.Property: newType.Members.Add(codeGenerationHelper.CreateProperty(pkClassName, pkRefProperty.MakeFirstCharUpperCase())); newType.Members.Add(codeGenerationHelper.CreateField(pkClassName, pkRefProperty.MakeFirstCharLowerCase())); break; case FieldGenerationConvention.AutoProperty: //pkPropertyName = columnDetail.ColumnName.GetFormattedText(); var codeMemberProperty = codeGenerationHelper.CreateAutoProperty(pkClassName, pkRefProperty); newType.Members.Add(codeMemberProperty); break; default: newType.Members.Add(codeGenerationHelper.CreateField(pkClassName, pkRefProperty)); break; } // create pk columns foreach (var columnDetail in pkColumns) { string pkPropertyName = columnDetail.ColumnName.GetPreferenceFormattedText(applicationPreferences); Type pkMapFromDbType = mapper.MapFromDBType(columnDetail.DataType, columnDetail.DataLength, columnDetail.DataPrecision, columnDetail.DataScale); // get compile unit of compile class switch (applicationPreferences.FieldGenerationConvention) { case FieldGenerationConvention.Property: newPKType.Members.Add(codeGenerationHelper.CreateProperty(pkMapFromDbType, pkPropertyName.MakeFirstCharUpperCase())); newPKType.Members.Add(codeGenerationHelper.CreateField(pkMapFromDbType, pkPropertyName.MakeFirstCharLowerCase())); break; case FieldGenerationConvention.AutoProperty: pkPropertyName = columnDetail.ColumnName.GetFormattedText(); var codeMemberProperty = codeGenerationHelper.CreateAutoProperty(pkMapFromDbType, pkPropertyName); newPKType.Members.Add(codeMemberProperty); break; default: newPKType.Members.Add(codeGenerationHelper.CreateField(pkMapFromDbType, pkPropertyName)); break; } } newPKType.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable")); newPKType.CustomAttributes.Add(new CodeAttributeDeclaration("Validate")); newPKType.CustomAttributes.Add(new CodeAttributeDeclaration("DataContract")); foreach (var member in newPKType.Members) { if (member is CodeMemberProperty) { CodeMemberProperty property = (CodeMemberProperty)member; CodeAttributeArgument[] arguments = new CodeAttributeArgument[] { new CodeAttributeArgument("Name",new CodePrimitiveExpression(dataContractCount.ToString())), new CodeAttributeArgument("Order",new CodePrimitiveExpression(dataContractCount++)) }; property.CustomAttributes.Add(new CodeAttributeDeclaration("DataMember", arguments)); } } } } // create normal columns foreach (var columnDetail in normalColumns) { string propertyName = columnDetail.ColumnName.GetPreferenceFormattedText(applicationPreferences); Type mapFromDbType = mapper.MapFromDBType(columnDetail.DataType, columnDetail.DataLength, columnDetail.DataPrecision, columnDetail.DataScale); if(FiendInTableReference(columnDetail)) continue; switch (applicationPreferences.FieldGenerationConvention) { case FieldGenerationConvention.Property: newType.Members.Add(codeGenerationHelper.CreateProperty(mapFromDbType, propertyName.MakeFirstCharUpperCase())); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, propertyName.MakeFirstCharLowerCase())); break; case FieldGenerationConvention.AutoProperty: propertyName = columnDetail.ColumnName.GetFormattedText(); var codeMemberProperty = codeGenerationHelper.CreateAutoProperty(mapFromDbType, propertyName); newType.Members.Add(codeMemberProperty); break; default: newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, propertyName)); break; } } // create detail member if exist if(tableReferences != null && tableReferences.Count > 0) { foreach (KeyValuePair<string, TableReference> pair in tableReferences) { TableReference reference = pair.Value; string refTable = GlobalCache.Instance.ReplaceShortWords(reference.ReferenceTable); switch(reference.ReferenceType) { case ReferenceType.OneToMany: refTable = refTable.GetFormattedText(); var detailListMemberProperty = codeGenerationHelper.CreateAutoProperty("IList<" + refTable + ">", refTable + "s"); newType.Members.Add(detailListMemberProperty); break; case ReferenceType.ManyToOne: refTable = refTable.GetFormattedText(); var masterMemberProperty = codeGenerationHelper.CreateAutoProperty(refTable, refTable); newType.Members.Add(masterMemberProperty); break; } } } var constructor = new CodeConstructor {Attributes = MemberAttributes.Public}; newType.Members.Add(constructor); newType.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable")); newType.CustomAttributes.Add(new CodeAttributeDeclaration("Validate")); newType.CustomAttributes.Add(new CodeAttributeDeclaration("DataContract")); foreach (var member in newType.Members) { if(member is CodeMemberProperty) { CodeMemberProperty property = (CodeMemberProperty) member; CodeAttributeArgument[] arguments = new CodeAttributeArgument[] { new CodeAttributeArgument("Name",new CodePrimitiveExpression(dataContractCount.ToString())), new CodeAttributeArgument("Order",new CodePrimitiveExpression(dataContractCount++)) }; property.CustomAttributes.Add(new CodeAttributeDeclaration("DataMember",arguments)); } } pkClass = pkCompileUnit; return compileUnit; }
public CodeCompileUnit GetCompleteCompileUnit(string mapName) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, mapName); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = appPrefs.GeneratePartialClasses; var className = Formatter.FormatSingular(Table.Name); switch (appPrefs.Language) { case Language.CSharp: newType.BaseTypes.Add(string.Format("ClassMapping<{0}{1}>", appPrefs.ClassNamePrefix, className)); break; case Language.VB: newType.BaseTypes.Add(string.Format("ClassMapping(Of {0}{1})", appPrefs.ClassNamePrefix, className)); break; } var constructor = new CodeConstructor {Attributes = MemberAttributes.Public}; // Table Name - Only ouput if table is different than the class name. if (Table.Name.ToLower() != className.ToLower()) { constructor.Statements.Add(new CodeSnippetStatement(TABS + "Table(\"" + Table.Name + "\");")); } // Scheme / Owner Name if (!string.IsNullOrEmpty(Table.Owner)) { constructor.Statements.Add(new CodeSnippetStatement(TABS + "Schema(\"" + Table.Owner + "\");")); } constructor.Statements.Add(new CodeSnippetStatement(TABS + string.Format("Lazy({0});", appPrefs.UseLazy ? "true" : "false"))); var mapper = new DBColumnMapper(appPrefs); // Id or ComposedId Map if (Table.PrimaryKey != null) { if (UsesSequence) { constructor.Statements.Add( new CodeSnippetStatement(TABS + mapper.IdSequenceMap(Table.PrimaryKey.Columns[0], appPrefs.Sequence, Formatter))); } else if (Table.PrimaryKey.Type == PrimaryKeyType.PrimaryKey) { constructor.Statements.Add( new CodeSnippetStatement(TABS + mapper.IdMap(Table.PrimaryKey.Columns[0], Formatter))); } else if (Table.PrimaryKey.Type == PrimaryKeyType.CompositeKey) { var pkColumns = Table.PrimaryKey.Columns; constructor.Statements.Add( new CodeSnippetStatement(TABS + mapper.CompositeIdMap(pkColumns, Formatter))); } } // Property Map foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { constructor.Statements.Add(new CodeSnippetStatement(TABS + mapper.Map(column, Formatter, appPrefs.IncludeLengthAndScale))); } // Many To One Mapping foreach (var fk in Table.ForeignKeys.Where(fk => fk.Columns.First().IsForeignKey && appPrefs.IncludeForeignKeys)) { constructor.Statements.Add(new CodeSnippetStatement(mapper.Reference(fk, Formatter))); } // Bag Table.HasManyRelationships.ToList().ForEach(x => constructor.Statements.Add(new CodeSnippetStatement(mapper.Bag(x, Formatter)))); newType.Members.Add(constructor); // Strip out any semicolons if (appPrefs.Language == Language.VB) { foreach (CodeSnippetStatement statement in constructor.Statements) { statement.Value = statement.Value.Replace(";", string.Empty); } } 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 + "\");")); 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("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; }