Dynamic Data Store Delete Problem

Vote:
 

I have a problem with some code that is a couple of years old where an error is thrown when deleting an item from the dynamic data store. I didn't write the code originally but I'm sure it must have worked at some point for it to have been released.

The scenario is that we are using the dynamic data store to hold ratings of CMS pages and we have a reset feature at the page level to allow editors to reset the rating on a page by page basis.

The code that actually does the delete is as follows:

       //itemId is the CMS page ID

        public void DeleteRecordsForContent(string itemId)
        {
            DynamicDataStore store = this.DynamicDataStore;
            if (store != null)
            {
                foreach (TDataStoreItem storeItem in this.GetRecordsForContent(itemId))
                {
                    store.Delete(storeItem);
                }
            }
        }

At the call to store.Delete I get the error:

The object passed does not have an Id property of type EPiServer.Data.Identity or System.Guid and has not been loaded through this instance and therefore cannot be mapped to an identity.

Any thoughts?

Thanks in advance,

Mark

#79686
Dec 30, 2013 13:49
Vote:
 

can you post the code for GetRecordsForContent also?

#79706
Dec 30, 2013 21:38
Vote:
 

and for this.DynamicDataStore as well :)

In other words - please provide full fragment of source code for problematic area in order to find out an issue.

#79726
Jan 02, 2014 13:44
Vote:
 

Hello,

Code for GetRecordsForContent and this.DynamicDataStore below. Any thoughts or suggestions appreciated.

        public DynamicDataStore DynamicDataStore
        {
            get
            {
                DynamicDataStore dynamicDataStore = DynamicDataStoreFactory.Instance.GetStore(_dynamicDataStoreName);
                if (dynamicDataStore == null)
                {
                    dynamicDataStore = DynamicDataStoreFactory.Instance.CreateStore(_dynamicDataStoreName, typeof(TDataStoreItem));
                }
                return dynamicDataStore;
            }
        }


        public IEnumerable<TDataStoreItem> GetRecordsForContent(string itemId)
        {
            DynamicDataStore store = this.DynamicDataStore;

            if (store != null)
            {
                return
                    from TDataStoreItem record in store.Find(ContentItemKey, itemId)
                    select record;
            }
            
            return null;
        }

       //ContentItemKey is a string set to "ItemId"

#79785
Jan 07, 2014 9:57
Vote:
 

If it's possible try to keep dynamic store into a class member (for that you most probably will need to move type argument to class level):

 

using EPiServer.Data.Dynamic;

public class  ... <TDataStoreItem>
...
    private DynamicDataStore store;

    private DynamicDataStore Store
    {
        get
        {
            if (this.store == null)
            {
                this.store = typeof(TDataStoreItem).GetStore();
            }

            return this.store;
        }
    }

    

#79789
Jan 07, 2014 10:38
Vote:
 

what about just do 

foreach (var item in (from item in this.DynamicDataStore where item.ItemId==itemId select item))

{

item.Delete();

}

#79998
Jan 13, 2014 13:03
Vote:
 

Your code is probably failing for 2 reasons:

1. You don't implement an ID property like this on the object you are trying to store

public Identity Id { get; set; }

2. You are not using the same DynamicDataStoreInstance when you retrieve objects and when you try to delete them. These lines will get the first instance of DDS
----------------------
public void DeleteRecordsForContent(string itemId)
        {
            DynamicDataStore store = this.DynamicDataStore;
...but this line:
foreach (TDataStoreItem storeItem in this.GetRecordsForContent(itemId))
will call...
-----------------
 public IEnumerable<TDataStoreItem> GetRecordsForContent(string itemId)
        {
            DynamicDataStore store = this.DynamicDataStore;
...but DynamicDataStore property doesn't save the instance so you will get a new one...
--------------
public DynamicDataStore DynamicDataStore
        {
            get
            {
                DynamicDataStore dynamicDataStore = DynamicDataStoreFactory.Instance.GetStore(_dynamicDataStoreName);

Fix either of these and it should work...

#80008
Edited, Jan 13, 2014 15:39
Vote:
 

Thanks all for your responses, Daniel's helped me resolve the issue. I ended up passing the DynamicDataStore object into the method GetRecordsForContent to ensure I was using the correct instance. It's not that clean but it works!

Thanks again,

Mark

#80214
Jan 16, 2014 17:25
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.