My index id keep zero

Apr 22, 2011 at 6:29 AM
Edited Apr 22, 2011 at 10:09 AM

Hi, 

i have two table named categories and notes. when i try to add new entries to categories, the categoriesID keep zero. so when i add new one, it will replace the old one, because of the same id. 

 

My Table Definition

 

 protected override List<ITableDefinition> _RegisterTables()
        {
            return new List<ITableDefinition>
                       {    
                           
                           CreateTableDefinition<Category, int>(i => i.categoryID)
                               .WithIndex<Category, int, int>("categoryID", x => x.categoryID),
                           CreateTableDefinition<Notes, int>(i => i.noteID)
                              .WithIndex<Notes, int, int>("noteID", x => x.noteID)
                              .WithIndex<Notes, int, int>("categoryID", notes => notes.categoryID ),
                       };
}

 

My Identity trigger

public class IdentityTrigger<T> : BaseSterlingTrigger<T, int> where T : class, IBaseModel, new()
    {
        private static int _idx = 1;

		public IdentityTrigger(ISterlingDatabaseInstance database)
		{
			// if a record exists, set it to the highest value plus 1  
			if (database.Query<T, int>().Any())
			{
				_idx = database.Query<T, int>().Max(key => key.Key) + 1;
			}

            Debug.WriteLine("IdentityTrigger Called id ="+ _idx);
		}


        public override bool BeforeSave(T instance)
		{
			if (instance.Id < 1)
			{
				instance.Id = _idx++;
			}

			return true;
		}

		public override void AfterSave(T instance)
		{
			return;
		}

		public override bool BeforeDelete(int key)
		{
			return true;
		}

    }

    public interface IBaseModel
    {
        int Id { get; set; }
    }

My Sterling Service

Database = _engine.SterlingDatabase.RegisterDatabase<NotesDatabase>();
            

            SterlingService.Current.Database.RegisterTrigger(new IdentityTrigger<Category>(SterlingService.Current.Database));
            SterlingService.Current.Database.RegisterTrigger(new IdentityTrigger<Notes>(SterlingService.Current.Database));

Any suggestion ?

Thanks

Coordinator
Apr 22, 2011 at 11:52 AM

First, you don't have to have indexes for keys. that is redundant. The fact you defined the category id as a the key means there should not be an index because you can just query the key list. Otherwise, I'm not sure why the trigger isn't firing but I'd troubleshoot with logging and see what Sterling is doing and maybe step through the code. Take a look at the tests and see how your code differs from the test.

Apr 22, 2011 at 2:08 PM

Which test code do you mean ?

Apr 22, 2011 at 2:32 PM
Edited Apr 22, 2011 at 2:32 PM

this is my sterlingservice,

 

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using Wintellect.Sterling;

namespace App
{
    public sealed class SterlingService : IApplicationService, IApplicationLifetimeAware, IDisposable
    {
        
        public const long KILOBYTE = 1024;
        public const long MEGABYTE = 1024 * KILOBYTE;
        public const long QUOTA = 100 * MEGABYTE;
        

        public void Starting()
        {
            DateTime start = DateTime.Now;
            if (DesignerProperties.IsInDesignTool) return;
        
            _engine.Activate();
        
            // Put the type of the Database Class after RegisterDatabase<
            Database = _engine.SterlingDatabase.RegisterDatabase<NotesDatabase>();
            

            SterlingService.Current.Database.RegisterTrigger(new IdentityTrigger<Category>(SterlingService.Current.Database));
            SterlingService.Current.Database.RegisterTrigger(new IdentityTrigger<Notes>(SterlingService.Current.Database));


			//SterlingService.Current.Database.RegisterTrigger(new IdentityTrigger<Notes>(SterlingService.Current.Database));

            Debug.WriteLine("Starling Service Starting");
        }

        #region SterlingEngine
        private SterlingEngine _engine;

        public static SterlingService Current { get; private set; }

        public ISterlingDatabaseInstance Database { get; private set; }

        /// <summary>
        /// Called by an application in order to initialize the application extension service.
        /// </summary>
        /// <param name="context">Provides information about the application state. </param>
        public void StartService(ApplicationServiceContext context)
        {
            if (DesignerProperties.IsInDesignTool) return;
			DateTime start = DateTime.Now;
            _engine = new SterlingEngine();
            Current = this;
        }

        /// <summary>
        /// Called by an application in order to stop the application extension service. 
        /// </summary>
        public void StopService()
        {
            return;
        }

        public static void ExecuteOnUIThread(Action action)
        {
            if (Deployment.Current.CheckAccess())
            {
                var dispatcher = Deployment.Current.Dispatcher;
                if (dispatcher.CheckAccess())
                {
                    dispatcher.BeginInvoke(action);
                }
            }
            else
            {
                action();
            }
        }

        /// <summary>
        /// Called by an application immediately after the <see cref="E:System.Windows.Application.Startup"/> event occurs.
        /// </summary>
        public void Started()
        {
            return;
        }

        /// <summary>
        /// Called by an application immediately before the <see cref="E:System.Windows.Application.Exit"/> event occurs. 
        /// </summary>
        public void Exiting()
        {
            if (DesignerProperties.IsInDesignTool) return;
        }

        /// <summary>
        /// Called by an application immediately after the <see cref="E:System.Windows.Application.Exit"/> event occurs. 
        /// </summary>
        public void Exited()
        {
            Dispose();
            _engine = null;
            return;
        }

        public void RequestRebuild()
        {
            if (RebuildRequested != null)
            {
                RebuildRequested(this, EventArgs.Empty);
            }
        }

        public event EventHandler RebuildRequested;

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (_engine != null)
            {
                _engine.Dispose();
            }
            GC.SuppressFinalize(this);
        } 
        #endregion
    }
    
}

Anything wrong with it ?

Apr 22, 2011 at 7:29 PM
Edited Apr 23, 2011 at 7:39 AM

Sorry, i already figure it out. 

Thanks For Your help.

Aug 10, 2011 at 7:03 PM

Can you share the solution?