使用 Fluent nHibernate 将 SQL Server 时间戳列自动映射到 byte[]

发布于 2024-09-14 18:14:30 字数 1482 浏览 1 评论 0原文

我开始在一个项目中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东北女汉子 2024-09-21 18:14:30

在一无所获之后,我决定单步执行 Fluent nHibernate 代码来看看发生了什么。然后,我找到了“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)
        };
    }
}

请注意 MyChangeCheckVersionStep,这是不同的。然后,我实现了一个 VersionStep 类,该类将 ChangeCheck 添加到假定为版本列的列名称列表中:

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

该类基本上调用默认的 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:

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. I then implemented a VersionStep class that added ChangeCheck to the list of column names that are assumed to be Version columns:

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文