public EntityWriter(DatabaseSchema schema, CodeWriterSettings codeWriterSettings) { this.schema = schema; this.codeWriterSettings = codeWriterSettings; PrepareSchemaNames.Prepare(schema, this.codeWriterSettings.Namer); dataAnnotationWriter = new DataAnnotationWriter(false, codeWriterSettings); mappingNamer = new MappingNamer(); }
/// <summary> /// Writes the C# code of the table /// </summary> /// <returns></returns> public string Write() { var codeTarget = _codeWriterSettings.CodeTarget; _dataAnnotationWriter = new DataAnnotationWriter(IsEntityFramework(), _codeWriterSettings); var className = _table.NetName; if (string.IsNullOrEmpty(className) && _table.DatabaseSchema != null) { PrepareSchemaNames.Prepare(_table.DatabaseSchema, _codeWriterSettings.Namer); className = _table.NetName; } _dataTypeWriter.CodeTarget = codeTarget; _inheritanceTable = _table.FindInheritanceTable(); WriteNamespaces(); _codeWriterSettings.CodeInserter.WriteNamespaces(_table, _cb); if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace)) { _cb.BeginNest("namespace " + _codeWriterSettings.Namespace); } if (codeTarget == CodeTarget.PocoRiaServices) { WriteRiaClass(className); } else { var tableOrView = _table is DatabaseView ? "view" : "table"; var comment = "Class representing " + _table.Name + " " + tableOrView; var classDefinition = "public class " + className; if (_inheritanceTable != null) { classDefinition += " : " + _inheritanceTable.NetName; } _codeWriterSettings.CodeInserter.WriteTableAnnotations(_table, _cb); using (_cb.BeginNest(classDefinition, comment)) { WriteClassMembers(className); } } if (_table.HasCompositeKey && _inheritanceTable == null) { WriteCompositeKeyClass(className); } if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace)) { _cb.EndNest(); } return(_cb.ToString()); }
/// <summary> /// Writes the C# code of the table /// </summary> /// <returns></returns> public string Write() { var codeTarget = _codeWriterSettings.CodeTarget; _dataAnnotationWriter = new DataAnnotationWriter(IsEntityFramework(), _codeWriterSettings); var className = _table.NetName; if (string.IsNullOrEmpty(className) && _table.DatabaseSchema != null) { PrepareSchemaNames.Prepare(_table.DatabaseSchema, _codeWriterSettings.Namer); className = _table.NetName; } _dataTypeWriter.CodeTarget = codeTarget; _inheritanceTable = _table.FindInheritanceTable(); WriteNamespaces(); _codeWriterSettings.CodeInserter.WriteNamespaces(_table, _cb); if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace)) { _cb.BeginNest("namespace " + _codeWriterSettings.Namespace); } if (codeTarget == CodeTarget.PocoRiaServices) { WriteRiaClass(className); } else { var tableOrView = _table is DatabaseView ? "view" : "table"; var comment = "Class representing " + _table.Name + " " + tableOrView; var classDefinition = "public class " + className; if (_inheritanceTable != null) { classDefinition += " : " + _inheritanceTable.NetName; } _codeWriterSettings.CodeInserter.WriteTableAnnotations(_table, _cb); using (_cb.BeginNest(classDefinition, comment)) { WriteClassMembers(className); } } if (_table.HasCompositeKey && _inheritanceTable == null) { WriteCompositeKeyClass(className); } if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace)) { _cb.EndNest(); } return _cb.ToString(); }
/// <summary> /// Writes the C# code of the table /// </summary> /// <returns></returns> public string Write() { var codeTarget = _codeWriterSettings.CodeTarget; _dataAnnotationWriter = new DataAnnotationWriter(IsEntityFramework(), _codeWriterSettings); var className = _table.NetName; if (string.IsNullOrEmpty(className) && _table.DatabaseSchema != null) { PrepareSchemaNames.Prepare(_table.DatabaseSchema); className = _table.NetName; } _dataTypeWriter.CodeTarget = codeTarget; WriteNamespaces(); if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace)) { _cb.BeginNest("namespace " + _codeWriterSettings.Namespace); } if (codeTarget == CodeTarget.PocoRiaServices) { WriteRiaClass(className); } else { var tableOrView = _table is DatabaseView ? "view" : "table"; using (_cb.BeginNest("public class " + className, "Class representing " + _table.Name + " " + tableOrView)) { WriteClassMembers(className); } } if (_table.HasCompositeKey) { WriteCompositeKeyClass(className); } if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace)) { _cb.EndNest(); } return(_cb.ToString()); }
public void TestRequiredWithErrorMessageFormat() { //arrange var settings = new CodeWriterSettings(); settings.RequiredErrorMessage = "{0} is mandatory"; var classBuilder = new ClassBuilder(); var column = new DatabaseColumn(); column.Name = column.NetName = "Important"; column.DataType = new DataType("NVARCHAR2", "System.String"); column.Nullable = false; var target = new DataAnnotationWriter(true, settings); //act target.Write(classBuilder, column); var result = classBuilder.ToString().Trim(); //ignore lines //assert Assert.AreEqual("[Required(ErrorMessage=\"Important is mandatory\")]", result); }
public void TestIndex() { //arrange var settings = new CodeWriterSettings(); settings.CodeTarget = CodeTarget.PocoEntityCodeFirst; settings.WriteCodeFirstIndexAttribute = true; var classBuilder = new ClassBuilder(); var table = new DatabaseTable { Name = "Test" }; var nameColumn = table.AddColumn<int>("Id").AddPrimaryKey() .AddColumn<string>("Name").AddNullable().AddIndex("IX_NAME"); var target = new DataAnnotationWriter(true, settings); //act target.Write(classBuilder, nameColumn); var result = classBuilder.ToString().Trim(); //ignore lines //assert Assert.AreEqual("[Index(\"IX_NAME\")]", result); }
public void TestDecimalRangeWithErrorMessage() { //arrange var settings = new CodeWriterSettings(); settings.RangeErrorMessage = "{1} must be less than {0}"; var classBuilder = new ClassBuilder(); var column = new DatabaseColumn(); column.Name = column.NetName = "Name"; column.DataType = new DataType("NUMBER", "System.Decimal"); column.Nullable = true; column.Precision = 5; column.Scale = 1; var target = new DataAnnotationWriter(true, settings); //act target.Write(classBuilder, column); var result = classBuilder.ToString().Trim(); //ignore lines //assert Assert.AreEqual("[Range(typeof(decimal), \"0\", \"9999\", ErrorMessage=\"Name must be less than 9999\")]", result); }
public void TestStringLength() { //arrange var settings = new CodeWriterSettings(); settings.StringLengthErrorMessage = null; var classBuilder = new ClassBuilder(); var column = new DatabaseColumn(); column.Name = column.NetName = "Name"; column.DataType = new DataType("NVARCHAR2", "System.String"); column.Nullable = true; column.Length = 10; var target = new DataAnnotationWriter(true, settings); //act target.Write(classBuilder, column); var result = classBuilder.ToString().Trim(); //ignore lines //assert Assert.AreEqual("[StringLength(10)]", result); }
public void TestIndexNotNeededForPrimaryKey() { //arrange var settings = new CodeWriterSettings(); settings.CodeTarget = CodeTarget.PocoEntityCodeFirst; settings.WriteCodeFirstIndexAttribute = true; var classBuilder = new ClassBuilder(); var table = new DatabaseTable { Name = "Test" }; table.AddColumn<int>("Id").AddPrimaryKey().AddIndex("PK_TEST") .AddColumn<string>("Category").AddNullable() .AddColumn<string>("Name").AddNullable().AddIndex("IX_NAME") ; var idColumn = table.PrimaryKeyColumn; var target = new DataAnnotationWriter(true, settings); //act target.Write(classBuilder, idColumn); var result = classBuilder.ToString().Trim(); //ignore lines //assert Assert.IsTrue(result.IndexOf("[Index", StringComparison.OrdinalIgnoreCase) == -1, "Should be just[Key]"); }
public void TestRangeWithErrorMessageFormat1() { //arrange var settings = new CodeWriterSettings(); settings.RangeErrorMessage = "Must be less than {0}"; var classBuilder = new ClassBuilder(); var column = new DatabaseColumn(); column.Name = column.NetName = "Name"; column.DataType = new DataType("INT", "System.Int32"); column.Nullable = true; column.Precision = 5; column.Scale = 0; var target = new DataAnnotationWriter(true, settings); //act target.Write(classBuilder, column); var result = classBuilder.ToString().Trim(); //ignore lines //assert Assert.AreEqual("[Range(0, 99999, ErrorMessage=\"Must be less than 99999\")]", result); }