3

在使用 Episerver 中的动态数据存储持久化实体之前为实体指定给定 ID 时,实体不会持久化。根据我看到的教程,这应该没问题。有什么线索吗?不抛出异常,并且 Save 调用返回我指定的实体的 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;
4

1 回答 1

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>
}

它有效

在此处输入图像描述

显然,您应该根据自己的模式进行调整,如果它不起作用,则可能是您的数据库用户权限不足。

于 2016-11-18T16:25:36.317 回答