gpt4 book ai didi

nhibernate - Nhibernate 和 Fluent Nhibernate 的只读策略

转载 作者:行者123 更新时间:2023-12-03 16:27:46 26 4
gpt4 key购买 nike

我一直在为初学者阅读 nhibernate 3.0 并阅读了一些常见错误(我犯了一些错误)

我想知道将一个或多个记录设为只读有哪些策略。现在我取回所有行并循环遍历它们,使它们通过 session.Readonly() 成为只读。

我喜欢在书中他们用流利的方式做的

class EntityMap : ClassMap<Entity>
{
public EntityMap()
{
SchemaAction.None();
ReadOnly();

// Mappings
}
}

我不确定的是,如果我需要这些记录不是只读的,会发生什么?对我来说,这意味着我必须制作完全相同的映射减去那两行代码。

所以我想这样做并拥有 ReadonlyEntityMap 和 EntityMap 但我宁愿不必将所有设置复制两次。

任何人都有关于如何做到这一点的想法?或者更好的 readonly 想法?

最佳答案

使用 ReadOnly()当您修改对象的属性时,您的映射将禁用对数据库的任何更新。

如果你想应用某种只读和可写策略,我建议使用 Fluent Convention和标记界面或类似的。

您也可以申请ReadOnly()如果您只想禁用对该属性的写入,则将其写入一个属性。我使用该技术来避免写回 SQL Server 中的计算列。

像这样的东西:

public interface IReadOnly{} //Mark entities with this interface

public class ReadOnlyConvention
: IClassConvention, IClassConventionAcceptance<IClassInspector>
{
public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
{
criteria.Expect(x => x.EntityType Is IReadOnly);
}

public void Apply(IClassInstance instance)
{
instance.ReadOnly();
}
}

更新:
如果您想做时间转换,我建议为您的 DateTime 对象创建一个 IUserType,该对象在不修改基础数据的情况下转换为用户时间。
  • Date Time Support in NHibernate
  • Custom IUserType for DateTime
  • 关于nhibernate - Nhibernate 和 Fluent Nhibernate 的只读策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7420085/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com