使用 Fluent nHibernate 将 SQL Server 时间戳列自动映射到 byte[]
我开始在一个项目中使用 Fluent nHibernate,并尝试让自动映射发挥作用。当时,我一直致力于将数据库的时间戳字段映射到字节数组。我们使用 SQL Server 2008 作为数据库,并且我们不是从代码生成数据库。
我所拥有的:
public class Entity
{
public virtual Guid RowID { get; protected set; }
public virtual byte[] ChangeCheck { get; protected set; }
public virtual string Data { get; set; }
}
我们的数据库约定是将版本字段命名为“ChangeCheck”。我似乎无法找到覆盖 DefaultAutomappingConfiguration 的默认行为以使用 ChangeCheck 作为自动生成的版本字段的位置。
是否可以获取 DefaultAutomappingConfiguration 子类来将所有 ChangeCheck 字段自动映射到版本字段中?
感谢您的指点和帮助。
可选解决方案:
鉴于我使用“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>();
哪个有效,但是我无法弄清楚如何删除覆盖以获取 ChangeCheck 列设置作为我的版本列,而不必覆盖我的所有实体。
I am starting to use Fluent nHibernate on a project and am trying to get the automapping to work. 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.
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'. I cannot seem to locate where I override the default behaviour of DefaultAutomappingConfiguration to use ChangeCheck as the auto-generated version field.
Is it possible to get a DefaultAutomappingConfiguration sub-class to automap all ChangeCheck fields into version fields?
Thanks for any pointers and help.
Optional solution:
Given I create an automap override for all entities using 'ChangeCheck' I could do the following:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在一无所获之后,我决定单步执行 Fluent nHibernate 代码来看看发生了什么。然后,我找到了“DefaultAutomappingConfiguration”的设置方式,并创建了我自己的版本:
请注意 MyChangeCheckVersionStep,这是不同的。然后,我实现了一个 VersionStep 类,该类将 ChangeCheck 添加到假定为版本列的列名称列表中:
该类基本上调用默认的 VersionStep > 除了 ShouldMap 之外的所有内容的实现。
现在,我不再需要为每个实体创建覆盖即可使版本正常工作。另请注意,我仍然使用 ChangeCheckVersionConvention - 它是我不再需要的每个实体类的覆盖。
After not getting anywhere I decided to step through the Fluent nHibernate code to see what was happening. I then located how the 'DefaultAutomappingConfiguration' was setup, and created my own version:
Note the MyChangeCheckVersionStep which is what is different. I then implemented a VersionStep class that added ChangeCheck to the list of column names that are assumed to be Version columns:
The class basically calls the defualt VersionStep implementation for everything but ShouldMap.
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.