3

When specifying a given id to an entity before persisting it using a dynamic data store in episerver, the entity is not persisted. According to tutorials I have seen this should have been just fine. Any clues? No exceptions are thrown, and the Save call returns the Id of the entity I specified the id of.

var winnerEntity = new DailyXMasLotteryWinner
{
    Id = Identity.NewIdentity(guid),
    FullName = fullName,
    Email = email,
    Phone = phone,
    WinnerTime = DateTime.Now
}

winnerStore.Save(winnerEntity); // does not persist to database!
winnerStore.Items().Count() == 0;
winnerEntity.Id = null;
winnerStore.Save(winnerEntity); // persists to database just fine!
winnerStore.Items().Count() == 1;
oligofren
  • 20,744
  • 16
  • 93
  • 180

1 Answers1

0

I'm not aware of your patterns but this is how I typically implement DDS

using System;
using System.Linq;
using EPiServer.Data;
using EPiServer.Data.Dynamic;
using EPiServer.ServiceLocation;

namespace Herlitz.EPiBlog.Models.DDS
{

    public interface IDailyXMasLotteryWinner
    {
        DailyXMasLotteryWinner Create(string fullName);

        DailyXMasLotteryWinner Get(string fullName);

        bool Exist(string property, string value);
    }

    [ServiceConfiguration(typeof(IDailyXMasLotteryWinner))]
    public class DailyXMasLotteryWinner : IDailyXMasLotteryWinner //,IDynamicData
    {
        private readonly DynamicDataStore _store = DynamicDataStoreFactory.Instance.CreateStore(typeof(DailyXMasLotteryWinner));

        public Identity Id { get; set; }

        public string FullName { get; set; }

        public DailyXMasLotteryWinner Create(string fullName)
        {
            if (Exist(property: "FullName", value: fullName))
            {
                return Get(fullName: fullName);
            }

            Id = Identity.NewIdentity(Guid.NewGuid());
            FullName = fullName;

            _store.Save(this);
            return this;
        }

        public DailyXMasLotteryWinner Get(string fullName)
        {
            return _store.Find<DailyXMasLotteryWinner>(propertyName: "FullName", value: fullName).FirstOrDefault();
        }

        public bool Exist(string property, string value)
        {
            return _store.Find<DailyXMasLotteryWinner>(propertyName: property, value: value).Any();
        }
    }
}

Tested with this

@{

    var xmasLocator = ServiceLocator.Current.GetInstance<IDailyXMasLotteryWinner>();
    var winner = xmasLocator.Create(fullName: "Ned Flanders");

    <div>@winner.Id @winner.FullName</div>
}

And it works

enter image description here

Obviously you should adapt this to your patterns, if it doesn't work it would probably be your database user that has insufficient rights.

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157