简体   繁体   English

动态属性.Not.Insert()。Not.Update()映射Fluent NHibernate

[英]Dynamically property .Not.Insert().Not.Update() Mapping Fluent NHibernate

im using the Fluent NHibernate together with the automapping functionality! 我将Fluent NHibernate与自动映射功能一起使用!

I'm currently using the follwing mapping statement to prevent autogenerated column values to be updated / insert 我目前正在使用以下映射语句来防止自动生成的列值被更新/插入

.Override<Entity>(map => map.Map(d => d.STATUS).Not.Insert().Not.Update())

It works fine so far, but now im looking for a way to get this solved more dynamically. 到目前为止,它运行良好,但现在我正在寻找一种方法来更动态地解决此问题。

What i would like to do is: 我想做的是:

I want to declare a custom attribute called [ReadOnlyDbField] and then declare all properties of the entity with this custom attribute to say: Just read this value and do not update / insert it. 我想声明一个名为[ReadOnlyDbField]的自定义属性,然后用该自定义属性声明该实体的所有属性,说:只读取该值,而不更新/插入它。

Then i want to tell the mapping configuration: 然后我想告诉映射配置:

Map all properties with the custom attribute [ReadOnlyDbField] to Not.Insert().Not.Update()

Is there a way to get this? 有办法吗?

Thanks 谢谢

Daniel 丹尼尔

You must create an attribute class 您必须创建一个属性类

public class NoInsertUpdateAttribute : Attribute
{

}

and another class for its convention: 以及另一个约定的类:

public class NoInsertUpdateConvention : AttributePropertyConvention<NoInsertUpdateAttribute>
{
    protected override void Apply(NoInsertUpdateAttribute attribute, IPropertyInstance instance)
    {
        instance.Not.Insert();
        instance.Not.Update();
    }
}

and add container assembly of NoInsertUpdateConvention class to automap: 并将NoInsertUpdateConvention类的容器程序集添加到自动映射:

var fluentConfiguration = Fluently
            .Configure()
            .Mappings(
                m => {
                    var autoMap = AutoMap
                        .Conventions.AddFromAssemblyOf<NoInsertUpdateConvention>()
                }
            );

finally add attribute to property you do not want to be inserted or updated: 最后,将属性添加到您不想插入或更新的属性中:

    [NoInsertUpdate]
    public virtual int? AccountID { get; set; }

Have a look at Generated method 看一下生成方法

Map(x => x.Status).Generated.Never(); // or Insert() or Always()

For auto application look at conventions , some examples here . 对于自动应用程序,请查看约定这里有一些示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM