Generate wrong field order after refresh schema

Posts   
 
    
Barry
User
Posts: 232
Joined: 17-Aug-2005
# Posted on: 23-Oct-2006 04:49:02   

I'm using version 2.0 (17 Oct, 2006) and adapter template with sql server 2005.

I've changed the order of a column in database, and refreshed its schema in LLBLGen designer. I found some bugs in generated files and cause exception during save.

File before refreshing schema


ConstantsEnums.cs
public enum TestEntityFieldIndex:int
{
    Field1,
    Field2,
    Field3,
    Field4,
    Field5
}

FieldInfoProvider.cs
private void InitTestEntityInfos()
{
    base.AddElementFieldInfo("TestEntity", "Field1", typeof(System.String), true, false, false, false,  (int)TestFieldIndex.Field1, 16, 0, 0);
    base.AddElementFieldInfo("TestEntity", "Field2", typeof(System.Int32), true, false, false, false,  (int)TestFieldIndex.Field2, 0, 0, 10);
    base.AddElementFieldInfo("TestEntity", "Field3", typeof(System.String), true, false, false, false,  (int)TestFieldIndex.Field3, 16, 0, 0);
    base.AddElementFieldInfo("TestEntity", "Field4", typeof(System.Decimal), true, false, false, false,  (int)TestFieldIndex.Field4, 0, 12, 2);
    base.AddElementFieldInfo("TestEntity", "Field5", typeof(System.DateTime), true, false, false, false,  (int)TestFieldIndex.Field5, 0, 0, 0);
}

PersistenceInfoProvider.cs
private void InitCostSheetDetailEntityMappings()
{
    base.AddElementMapping( "TestEntity", "dbTest", @"dbo", "TEST", 5 );
    base.AddElementFieldMapping( "TestEntity", "Field1", "Field1", false, (int)SqlDbType.NVarChar, 16, 0, 0, false, "", null, typeof(System.String), 0 );
    base.AddElementFieldMapping( "TestEntity", "Field2", "Field2", false, (int)SqlDbType.Int, 16, 0, 0, false, "", null, typeof(System.Int32), 1 );
    base.AddElementFieldMapping( "TestEntity", "Field3", "Field3", false, (int)SqlDbType.NVarChar, 16, 0, 0, false, "", null, typeof(System.String), 2 );
    base.AddElementFieldMapping( "TestEntity", "Field4", "Field4", false, (int)SqlDbType.Decimal, 16, 0, 0, false, "", null, typeof(System.Decimal), 3 );
    base.AddElementFieldMapping( "TestEntity", "Field5", "Field5", false, (int)SqlDbType.DateTime, 16, 0, 0, false, "", null, typeof(System.DateTime), 4 );
}

I moved "Field3" from the 3rd field to 5th field in database, and then refresh schema, it generated the following code.


ConstantsEnums.cs
public enum TestEntityFieldIndex:int
{
    Field1,
    Field2,
    Field4,
    Field5,
    Field3
}

FieldInfoProvider.cs
private void InitTestEntityInfos()
{
    base.AddElementFieldInfo("TestEntity", "Field1", typeof(System.String), true, false, false, false,  (int)TestFieldIndex.Field1, 16, 0, 0);
    base.AddElementFieldInfo("TestEntity", "Field2", typeof(System.Int32), true, false, false, false,  (int)TestFieldIndex.Field2, 0, 0, 10);
    base.AddElementFieldInfo("TestEntity", "Field4", typeof(System.Decimal), true, false, false, false,  (int)TestFieldIndex.Field4, 0, 12, 2);
    base.AddElementFieldInfo("TestEntity", "Field5", typeof(System.DateTime), true, false, false, false,  (int)TestFieldIndex.Field5, 0, 0, 0);
    base.AddElementFieldInfo("TestEntity", "Field3", typeof(System.String), true, false, false, false,  (int)TestFieldIndex.Field3, 16, 0, 0);
}

PersistenceInfoProvider.cs
private void InitCostSheetDetailEntityMappings()
{
    base.AddElementMapping( "TestEntity", "dbTest", @"dbo", "TEST", 5 );
    base.AddElementFieldMapping( "TestEntity", "Field1", "Field1", false, (int)SqlDbType.NVarChar, 16, 0, 0, false, "", null, typeof(System.String), 0 );
    base.AddElementFieldMapping( "TestEntity", "Field2", "Field2", false, (int)SqlDbType.Int, 16, 0, 0, false, "", null, typeof(System.Int32), 1 );
    base.AddElementFieldMapping( "TestEntity", "Field4", "Field4", false, (int)SqlDbType.Decimal, 16, 0, 0, false, "", null, typeof(System.Decimal), 3 );
    base.AddElementFieldMapping( "TestEntity", "Field5", "Field5", false, (int)SqlDbType.DateTime, 16, 0, 0, false, "", null, typeof(System.DateTime), 4 );
    base.AddElementFieldMapping( "TestEntity", "Field3", "Field3", false, (int)SqlDbType.NVarChar, 16, 0, 0, false, "", null, typeof(System.String), 2 );
}

When I try to save an entity, it throw an exception.


An exception was caught during the execution of an action query: Failed to convert parameter value from a String to a Decimal. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.

at SD.LLBLGen.Pro.ORMSupportClasses.ActionQuery.Execute()
at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.ExecuteActionQuery(IActionQuery queryToExecute)
at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.PersistQueue(List`1 queueToPersist, Boolean insertActions)
at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave, Boolean refetchAfterSave, IPredicateExpression updateRestriction, Boolean recurse)

Query: UPDATE [dbTest].[dbo].[TEST] SET [Field1]=@Field1,[Field2]=@Field2,[Field3]=@Field4,[Field4]=@Field5,[Field5]=@Field3 WHERE ( [dbTest].[dbo].[TEST].[Field1] = @Field1)
Parameter: @Field1 : String. Length: 16. Precision: 0. Scale: 0. Direction: Input. Value: "ABCDE".
Parameter: @Field2 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 12345.
Parameter: @Field4 : Decimal. Length: 0. Precision: 12. Scale: 2. Direction: Input. Value: "XYZ".
Parameter: @Field5 : DateTime. Length: 3. Precision: 0. Scale: 0. Direction: Input. Value: 0.789.
Parameter: @Field3 : String. Length: 16. Precision: 0. Scale: 0. Direction: Input. Value: "23/10/2006 10:32:20".

As you can see the order is changed in TestEntityFieldIndex, but order in other files are not changed, it break all the code as a result. cry

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 23-Oct-2006 11:14:09   

Looking into it.

(edit). Very strange indeed. The field indexes aren't updated. What's strange is that in project explorer, the order is correct, but in the entity editor it's not and the reason is that the field index, which should have been updated, isn't.

The reason I think is that the table field Field3 is mapped on, is found, so Field3 keeps its index, it's just mapped on another field. What's then of course wrong is that this causes problems in the code.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 23-Oct-2006 11:53:01   

Fixed it. The field index was corrected, but afterwards the fieldlist was sorted on ordinal of mapped field, This however caused that Field3 was moved to the end (as the mapped field was on ordinal 6), though the fieldindex stayed 3.

This has been corrected. It also works with gaps in the fields, so if you remove field2 from the entity (not from the table), then save, then move field 3 to the back in the table, then refresh, the indexes are still ascending and correct without gaps.

Fixed in next build.

Frans Bouma | Lead developer LLBLGen Pro