My linq update logic is not updating. help!
-
I have a database containing a key tracker table. The table has a single record with 2 fields, an alpha prefix and a numeric suffix. I get the record and perform all of my changes into the located record. Yet, inspite of doing a SubmitChanges, my table entry remains unchanged. My logic handles the records as follows:
lock ( threadLock ) { CrossConnectKeyTracker key = new CrossConnectKeyTracker( ); using ( MasterDataContext context = new MasterDataContext( connectionString ) ) { key = ( from k in context.CrossConnectKeyTrackers select k ).SingleOrDefault( ); if ( key == null ) { key = new CrossConnectKeyTracker( ); key.CharacterPrefix = "AA"; key.NumericSuffix = 00001; context.CrossConnectKeyTrackers.InsertOnSubmit( key ); context.SubmitChanges( ); return String.Concat( key.CharacterPrefix, key.NumericSuffix.ToString( ).PadLeft( 5, '0' ) ); } if ( key.NumericSuffix < 99999 ) key.NumericSuffix++; else { int value; if ( key.CharacterPrefix\[ 1 \] != 'Z' ) value = Convert.ToInt32( key.CharacterPrefix\[ 1 \] ); else value = Convert.ToInt32( key.CharacterPrefix\[ 0 \] ); value++; char newValue = Convert.ToChar( value ); if ( key.CharacterPrefix\[ 1 \] == 'Z' ) key.CharacterPrefix = string.Concat( newValue, key.CharacterPrefix\[ 1 \] ); else key.CharacterPrefix = string.Concat( key.CharacterPrefix\[ 0 \], newValue ); key.NumericSuffix = 00001; } context.SubmitChanges( ); } return String.Concat( key.CharacterPrefix, key.NumericSuffix.ToString( ).PadLeft( 5, '0' ) );
-
I have a database containing a key tracker table. The table has a single record with 2 fields, an alpha prefix and a numeric suffix. I get the record and perform all of my changes into the located record. Yet, inspite of doing a SubmitChanges, my table entry remains unchanged. My logic handles the records as follows:
lock ( threadLock ) { CrossConnectKeyTracker key = new CrossConnectKeyTracker( ); using ( MasterDataContext context = new MasterDataContext( connectionString ) ) { key = ( from k in context.CrossConnectKeyTrackers select k ).SingleOrDefault( ); if ( key == null ) { key = new CrossConnectKeyTracker( ); key.CharacterPrefix = "AA"; key.NumericSuffix = 00001; context.CrossConnectKeyTrackers.InsertOnSubmit( key ); context.SubmitChanges( ); return String.Concat( key.CharacterPrefix, key.NumericSuffix.ToString( ).PadLeft( 5, '0' ) ); } if ( key.NumericSuffix < 99999 ) key.NumericSuffix++; else { int value; if ( key.CharacterPrefix\[ 1 \] != 'Z' ) value = Convert.ToInt32( key.CharacterPrefix\[ 1 \] ); else value = Convert.ToInt32( key.CharacterPrefix\[ 0 \] ); value++; char newValue = Convert.ToChar( value ); if ( key.CharacterPrefix\[ 1 \] == 'Z' ) key.CharacterPrefix = string.Concat( newValue, key.CharacterPrefix\[ 1 \] ); else key.CharacterPrefix = string.Concat( key.CharacterPrefix\[ 0 \], newValue ); key.NumericSuffix = 00001; } context.SubmitChanges( ); } return String.Concat( key.CharacterPrefix, key.NumericSuffix.ToString( ).PadLeft( 5, '0' ) );
-
I have a database containing a key tracker table. The table has a single record with 2 fields, an alpha prefix and a numeric suffix. I get the record and perform all of my changes into the located record. Yet, inspite of doing a SubmitChanges, my table entry remains unchanged. My logic handles the records as follows:
lock ( threadLock ) { CrossConnectKeyTracker key = new CrossConnectKeyTracker( ); using ( MasterDataContext context = new MasterDataContext( connectionString ) ) { key = ( from k in context.CrossConnectKeyTrackers select k ).SingleOrDefault( ); if ( key == null ) { key = new CrossConnectKeyTracker( ); key.CharacterPrefix = "AA"; key.NumericSuffix = 00001; context.CrossConnectKeyTrackers.InsertOnSubmit( key ); context.SubmitChanges( ); return String.Concat( key.CharacterPrefix, key.NumericSuffix.ToString( ).PadLeft( 5, '0' ) ); } if ( key.NumericSuffix < 99999 ) key.NumericSuffix++; else { int value; if ( key.CharacterPrefix\[ 1 \] != 'Z' ) value = Convert.ToInt32( key.CharacterPrefix\[ 1 \] ); else value = Convert.ToInt32( key.CharacterPrefix\[ 0 \] ); value++; char newValue = Convert.ToChar( value ); if ( key.CharacterPrefix\[ 1 \] == 'Z' ) key.CharacterPrefix = string.Concat( newValue, key.CharacterPrefix\[ 1 \] ); else key.CharacterPrefix = string.Concat( key.CharacterPrefix\[ 0 \], newValue ); key.NumericSuffix = 00001; } context.SubmitChanges( ); } return String.Concat( key.CharacterPrefix, key.NumericSuffix.ToString( ).PadLeft( 5, '0' ) );
:laugh: It looks like you ran into exactly my issue. I have a keytracker table doing the same thing and it wasn't updating and I wanted to pull out my hair. Just change your table def as so: KeyFile primarykey int default 666 KeyPrefix ... KeySuffix ... I then always get my record with the 666 key and my update logic works! :doh: