[英]Automapping SQL Server timestamp columns to byte[] using Fluent nHibernate
I am starting to use Fluent nHibernate on a project and am trying to get the automapping to work. 我开始在一个项目上使用Fluent nHibernate,并试图使自动化工作。 At the time I am stuck with the mapping of our database's timestamp fields into byte-arrays.
当时我坚持将数据库的时间戳字段映射到字节数组。 We are using SQL Server 2008 as the database, and we are not generating the database from code.
我们使用SQL Server 2008作为数据库,我们不是从代码生成数据库。
What I have: 我有的:
public class Entity
{
public virtual Guid RowID { get; protected set; }
public virtual byte[] ChangeCheck { get; protected set; }
public virtual string Data { get; set; }
}
Our database convention is to name the version field 'ChangeCheck'. 我们的数据库约定是将版本字段命名为“ChangeCheck”。 I cannot seem to locate where I override the default behaviour of DefaultAutomappingConfiguration to use ChangeCheck as the auto-generated version field.
我无法找到我覆盖DefaultAutomappingConfiguration默认行为的位置,以使用ChangeCheck作为自动生成的版本字段。
Is it possible to get a DefaultAutomappingConfiguration sub-class to automap all ChangeCheck fields into version fields? 是否有可能获得DefaultAutomappingConfiguration子类以将所有ChangeCheck字段自动化为版本字段?
Thanks for any pointers and help. 感谢您的任何指示和帮助。
Given I create an automap override for all entities using 'ChangeCheck' I could do the following: 鉴于我使用'ChangeCheck'为所有实体创建了一个自动覆盖,我可以执行以下操作:
private class ChangeCheckVersionConvention : IVersionConvention
{
public void Apply(IVersionInstance instance)
{
instance.Column("ChangeCheck");
instance.Generated.Always();
instance.UnsavedValue(null);
}
}
public class EntityOverride : IAutoMappingOverride<IssueReport>
{
public void Override(AutoMapping<IssueReport> mapping)
{
mapping.Version(m => m.ChangeCheck);
}
}
//....
var persistenceModel = AutoMap.AssemblyOf<MyConfiguration>(new MyConfiguration())
.UseOverridesFromAssemblyOf<MyConfiguration>()
.Conventions.Add<ChangeCheckVersionConvention>();
Which works, however I cannot figure out how to remove the override to get the ChangeCheck column set-up as my Version column without having to override all my entities. 哪个有效,但我无法弄清楚如何删除覆盖以将ChangeCheck列设置为我的版本列,而不必覆盖我的所有实体。
After not getting anywhere I decided to step through the Fluent nHibernate code to see what was happening. 在没有到达任何地方后,我决定逐步完成Fluent nHibernate代码,看看发生了什么。 I then located how the 'DefaultAutomappingConfiguration' was setup, and created my own version:
然后我找到了如何设置'DefaultAutomappingConfiguration'并创建了我自己的版本:
public class MyAutomappingStoreConfiguration : DefaultAutomappingConfiguration
{
public override IEnumerable<IAutomappingStep> GetMappingSteps(AutoMapper mapper, IConventionFinder conventionFinder)
{
return new IAutomappingStep[]
{
new IdentityStep(this),
new MyChangeCheckVersionStep(this),
new ComponentStep(this, mapper),
new PropertyStep(conventionFinder, this),
new HasManyToManyStep(this),
new ReferenceStep(this),
new HasManyStep(this)
};
}
}
Note the MyChangeCheckVersionStep which is what is different. 注意MyChangeCheckVersionStep是不同的。 I then implemented a VersionStep class that added ChangeCheck to the list of column names that are assumed to be Version columns:
然后我实现了一个VersionStep类,它将ChangeCheck添加到假定为Version列的列名列表中:
public class MyChangeCheckVersionStep: IAutomappingStep
{
private static readonly IList<string> validNames
= new List<string> { "version", "timestamp", "changecheck" };
private static readonly IList<Type> validTypes
= new List<Type> { typeof(int), typeof(long), typeof(TimeSpan), typeof(byte[]) };
private readonly IAutomappingStep defaultVersionStep;
public MyChangeCheckVersionStep(IAutomappingConfiguration cfg)
{
this.defaultVersionStep = new VersionStep(cfg);
}
public bool ShouldMap(Member member)
{
return validNames.Contains(member.Name.ToLowerInvariant())
&& validTypes.Contains(member.PropertyType);
}
public void Map(ClassMappingBase classMap, Member member)
{
defaultVersionStep.Map(classMap, member);
}
}
The class basically calls the defualt VersionStep implementation for everything but ShouldMap . 除了ShouldMap之外 ,该类基本上调用了defualt VersionStep实现。
Now I no longer need to create overrides for each entity to get the Version working. 现在,我不再需要为每个实体创建覆盖以使版本正常工作。 Note as well, that I still use the ChangeCheckVersionConvention - it is the override on every entity class I no longer need.
还要注意,我仍然使用ChangeCheckVersionConvention - 它是我不再需要的每个实体类的覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.