我一直在尝试实现Martin Eden 的FrameLog。它看起来完全符合我认为自己编码的一些要求,但意识到可扩展性会受到限制并且需要很长时间。
所以我在我的 DbContext 中实现了 Save context 方法,使用了一个新的方法Save(string username)
来调用Logger.SaveChanges(username, SaveOptions.AcceptAllChangesAfterSave);
虽然这会将我的对象保存到数据库中,但我没有收到任何更改日志。有没有人有这方面的经验可以伸出援手?
这是我的课程:OppsContext.cs
public OppsContext()
: base("Name=OppsContext")
{
Logger = new FrameLogModule<ChangeSet, string>(new ChangeSetFactory(), FrameLogContext);
}
#region logging
public DbSet<ChangeSet> ChangeSets { get; set; }
public DbSet<ObjectChange> ObjectChanges { get; set; }
public DbSet<PropertyChange> PropertyChanges { get; set; }
public readonly FrameLogModule<ChangeSet, string> Logger;
public IFrameLogContext<ChangeSet, string> FrameLogContext
{
get { return new OppsContextAdapter(this); }
}
public HistoryExplorer<ChangeSet, string> HistoryExplorer
{
get { return new HistoryExplorer<ChangeSet, string>(FrameLogContext); }
}
public void Save(string author)
{
try
{
Logger.SaveChanges(author, SaveOptions.AcceptAllChangesAfterSave);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
OppsContextAdapter.cs
public class OppsContextAdapter : DbContextAdapter<ChangeSet, string>
{
private OppsContext context;
public OppsContextAdapter(OppsContext context)
: base(context)
{
this.context = context;
}
public override IQueryable<IChangeSet<string>> ChangeSets
{
get { return context.ChangeSets; }
}
public override IQueryable<IObjectChange<string>> ObjectChanges
{
get { return context.ObjectChanges; }
}
public override IQueryable<IPropertyChange<string>> PropertyChanges
{
get { return context.PropertyChanges; }
}
public override void AddChangeSet(ChangeSet changeSet)
{
context.ChangeSets.Add(changeSet);
}
public override Type UnderlyingContextType
{
get { return typeof(OppsContext); }
}
}