November Happy Hour will be moved to Thursday December 5th.

DynamicDataStore - only saves the last post

Vote:
 

I´m struggeling with the DynamicDataStore (CMS 6 R2 .NET 4.0) and can´t get it to work properly.

It saves my data alright, but when I save a new post it seems to over write the first one. When I try to open the store it always contains just one post.

Anyone?

 

This is the class that holds the data:

    [EPiServerDataStore(AutomaticallyCreateStore = true)]  
    public class ContactData 
    {
        public String IP { get; set; }
        public DateTime CreationDate { get; set; }
        public String Name { get; set; }
        public String Email{ get; set; }
        public String Phone1 { get; set; }
        public String Phone2 { get; set; }
        public String Message { get; set; }


        public EPiServer.Data.Identity Id { get; set; }
    }

    

 // This is the functions for saving and retreiving data

 

    public class ContactDataManager
    {
        protected static DynamicDataStore GetDynamicDataStore()
        {
            return DynamicDataStoreFactory.Instance.GetStore(typeof(ContactData)) ??
                  DynamicDataStoreFactory.Instance.CreateStore(typeof(ContactData));
        }

        public static void SaveContactData(ContactData contactDataToSave)
        {
            var store = GetDynamicDataStore();
            
            store.Save(contactDataToSave);

        }

        public static List<ContactData> GetAllContactData(EPiServer.Data.Identity Id)
        {

            var store = GetDynamicDataStore();
             var query = from contactData in store.Items<ContactData>() select contactData;

            return query.ToList();

        }

    }

    

#52688
Aug 09, 2011 23:30
Vote:
 

Hi Johan,

How do you set the Id property of your ContactData objects? If the objects have the same identity, the second one will overwrite the first one when saving to the DDS.

In this example the two ContactData objects have the same identity, so totalCount will be equal to 1:

Identity identity = Identity.NewIdentity(Guid.NewGuid());
ContactData contactData1 = new ContactData
{ 
Id = identity
// Other properties set
};

ContactData contactData2 = new ContactData
{
Id = identity
// Other properties set
};
            
ContactDataManager.SaveContactData(contactData1);
ContactDataManager.SaveContactData(contactData2); int totalCount = ContactDataManager.GetAllContactData().Count;

    

In this example the two ContactData objects have the different identities, so totalCount will be equal to 2:

Identity identity1 = Identity.NewIdentity(Guid.NewGuid());
Identity identity2 = Identity.NewIdentity(Guid.NewGuid());

ContactData contactData1 = new ContactData
{
Id = identity1
// Other properties set
};

ContactData contactData2 = new ContactData
{
Id = identity2
// Other properties set
};

ContactDataManager.SaveContactData(contactData1);
ContactDataManager.SaveContactData(contactData2);

int totalCount = ContactDataManager.GetAllContactData().Count;

    

Hopefully, this will help you out :)

Karoline

#52725
Aug 10, 2011 19:16
Vote:
 

Perfect! Of course ;)

 

Thanx!

#52729
Aug 10, 2011 21:16
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.