private void WriteColumn(DatabaseColumn column, bool notNetName) { var propertyName = PropertyName(column); var dataType = _dataTypeWriter.Write(column); if (notNetName) { //in EF, you want a fk Id property //must not conflict with entity fk name propertyName += "Id"; } _codeWriterSettings.CodeInserter.WriteColumnAnnotations(_table, column, _cb); var writeAnnotations = true; if (column.IsPrimaryKey && _codeWriterSettings.CodeTarget == CodeTarget.PocoEfCore && _table.PrimaryKey.Columns.Count > 1) { //EF Core doesn't like [Key] annotations on composite keys writeAnnotations = false; } if (writeAnnotations) { _dataAnnotationWriter.Write(_cb, column); } //for code first, ordinary properties are non-virtual. var useVirtual = !IsEntityFramework(); _cb.AppendAutomaticProperty(dataType, propertyName, useVirtual); }
private void WriteColumn(DatabaseColumn column) { var propertyName = column.NetName; // KL: Ensures that property name doesn't match class name if (propertyName == column.Table.NetName) { propertyName = string.Format("{0}Column", propertyName); } var dataType = _dataTypeWriter.Write(column); var isFk = column.IsForeignKey && column.ForeignKeyTable != null; if (isFk) { //KL: Returning if this is a foreign key. These should be written in a separate step. if (IsNHibernate()) { return; } if (IsEntityFramework() && (column.IsPrimaryKey || _codeWriterSettings.UseForeignKeyIdProperties)) { //if it's a primary key AND foreign key, CF requires a scalar property //optionally allow a shadow Id property to be created (convenient for CF) _cb.AppendAutomaticProperty(dataType, propertyName + "Id", true); } dataType = column.ForeignKeyTable.NetName; } _dataAnnotationWriter.Write(_cb, column); //for code first, ordinary properties are non-virtual. var useVirtual = (!IsEntityFramework() || isFk); _cb.AppendAutomaticProperty(dataType, propertyName, useVirtual); }
private void WriteColumn(DatabaseColumn column, bool notNetName) { var propertyName = CodeWriterUtils.GetPropertyNameForDatabaseColumn(column); var dataType = CodeWriterUtils.FindDataType(column); if (notNetName) { //in EF, you want a fk Id property //must not conflict with entity fk name propertyName += "Id"; } codeWriterSettings.CodeInserter.WriteColumnAnnotations(table, column, classBuilder); dataAnnotationWriter.Write(classBuilder, column, propertyName); var useVirtual = true; classBuilder.AppendAutomaticProperty(dataType, propertyName, useVirtual); }
private void WriteColumn(DatabaseColumn column, bool notNetName) { var propertyName = column.NetName; //in case the netName hasn't been set if (string.IsNullOrEmpty(propertyName)) { propertyName = column.Name; } // KL: Ensures that property name doesn't match class name if (propertyName == column.Table.NetName) { propertyName = string.Format("{0}Column", propertyName); } var dataType = _dataTypeWriter.Write(column); if (column.IsPrimaryKey && column.IsForeignKey) { //a foreign key will be written, so we need to avoid a collision var refTable = column.ForeignKeyTable; var fkDataType = refTable != null ? refTable.NetName : column.ForeignKeyTableName; if (fkDataType == propertyName) { notNetName = true; } } if (notNetName) { //in EF, you want a fk Id property //must not conflict with entity fk name propertyName += "Id"; } _codeWriterSettings.CodeInserter.WriteColumnAnnotations(_table, column, _cb); _dataAnnotationWriter.Write(_cb, column); //for code first, ordinary properties are non-virtual. var useVirtual = !IsEntityFramework(); _cb.AppendAutomaticProperty(dataType, propertyName, useVirtual); }
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); }