我不知道您的模式,但这就是我通常实施 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>
}
它有效
显然,您应该根据自己的模式进行调整,如果它不起作用,则可能是您的数据库用户权限不足。