Database.Load<Type>(key) does not work

Mar 17, 2011 at 12:42 PM

So i have a Method like this

       public static Item GetItem(Guid guid)
        {
            Item itemFound = App.Database.Load<Item>(guid);

            if (itemFound == null)
                throw new Exception("Item not found");
            else
                return itemFound;
        }

This does not work, itemFound ends up beng null but i know the object is there. It works if i used a linq query though like this 

        public static Item GetItem(Guid guid)
        {
            var item = (from i in App.Database.Query<Item, Guid>()
                        where i.LazyValue.Value.Guid == guid
                        select i.LazyValue.Value).FirstOrDefault();

            Item itemFound = item;

            if (itemFound == null)
                throw new Exception("Item not found");
            else
                return itemFound;
        }

Is this not working because i am using a Guid for my key?

Coordinator
Mar 17, 2011 at 2:12 PM

Guid should be fine as a key, I do it all of the time.

1. What version of Sterling - the release or a recent change set?

2. Can you show me your table definition with the lambda expression, and is the Guid declared as nullable?

3. Are you flushing after save?

4. Does it never load, or is it a particular record that doesn't load?

5. Is it possible to create a standalone test that duplicates the issue?

Mar 17, 2011 at 11:09 PM

After i posted this i found i have the same issue when deleting an item. No exception just the item is not removed from the database. 

1. What version of Sterling - the release or a recent change set?

Runtime version: v2.0.50727
Version:  1.0.4091.40870

2. Can you show me your table definition with the lambda expression, and is the Guid declared as nullable?

        protected override System.Collections.Generic.List<ITableDefinition> _RegisterTables()
        {
            return new List<ITableDefinition>
                       {
                             CreateTableDefinition<Item, Guid>(item => item.Guid)
                       };
        }

The Guid Property implemention is

        public Item()
        {
            _guid = System.Guid.NewGuid();
        }

        private Guid _guid;

        public Guid Guid
        {
            get
            {
                return _guid;
            }
        }

3. Are you flushing after save?

Yes, i save 3 sample items and then call the Flush method.

4. Does it never load, or is it a particular record that doesn't load?

No it seems to be any record.

5. Is it possible to create a standalone test that duplicates the issue?

I wrote a standalone test but it fails to start the engine, i'm pretty sure this is because its a normal test project and not a WP7 unit test. If you still need it i'll have to look into how to create WP7 unit tests. I haven't tried this yet. i get this exception "Unable to determine application identity of the caller."

Coordinator
Mar 17, 2011 at 11:17 PM

The problem is with your property definition. Sterling isn't able to set the Guid because the Guid is set as a readonly property. For Sterling to access any property, it must be a publicly accessible get (to read) and set (to set when it is de-serialized).

 

Mar 18, 2011 at 4:00 AM

Ah your a legend, so glad i got that working, i really needed a good database solution. 

so any property i want to serialize has to have a public set as well? 

Coordinator
Mar 18, 2011 at 9:05 AM

Tha'ts correct - this link describes more of the rules/pattern behind saving:

http://www.sterlingdatabase.com/sterling-user-guide/4-databases/c-saving-instances

Sterling has to be able to discover the property and then create an empty instance of the object and set the property to load it back.