指定id时DDS不保存实体

时间:2016-11-18 12:53:08

标签: episerver episerver-7

在使用episerver中的动态数据存储将其保持为实体之前为实体指定给定ID时,实体不会保留。根据教程,我看到这应该没问题。有线索吗?不会抛出任何异常,并且Save调用返回我指定id的实体的Id。

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;

1 个答案:

答案 0 :(得分:0)

我不知道你的模式,但这就是我通常实现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();
        }
    }
}

使用此

进行测试
@{

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

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

它有效

enter image description here

显然你应该对你的模式进行调整,如果它不起作用,那么你的数据库用户可能没有足够的权利。