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.
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"
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;
}
}
what about just do
foreach (var item in (from item in this.DynamicDataStore where item.ItemId==itemId select item))
{
item.Delete();
}
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...
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