2

私はこの質問が何度も同じような形で提起されたことを知っていますが、どのスレッドも私の質問に対する具体的な答えを私に与えることができませんでした。

Fluent NHibernateとFluentの自動マッピングを使用して、ドメインエンティティをマッピングします。現在、私はこの規則クラスを使用して、すべてのプロパティをNULL以外に設定しています。

public class NotNullColumnConvention : IPropertyConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IPropertyInstance instance)
    {
        instance.Not.Nullable();
    }
} 

大きな問題は次のとおりです。

エンティティクラスの単一のプロパティをNULLにするには、何をする必要がありますか?

これが私のエンティティクラスの1つです。

public class Employee : Entity
{
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}

誰かが最終的に私を助けてくれるなら、私は本当に嬉しいです!Googleのリターンページに入力した可能性のあるすべての検索文字列で、既にアクセス済みとしてマークされています...

ありがとう、
アルネ

編集:タイトルを変更しました...単一のプロパティにNULLを許可したい

4

1 に答える 1

4

属性を作成します:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class CanBeNullAttribute : Attribute
{
}

そして慣習:

public class CanBeNullPropertyConvention : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(
            x => !this.IsNullableProperty(x)
            || x.Property.MemberInfo.GetCustomAttributes(typeof(CanBeNullAttribute), true).Length > 0);
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.Nullable();
    }

    private bool IsNullableProperty(IExposedThroughPropertyInspector target)
    {
        var type = target.Property.PropertyType;

        return type.Equals(typeof(string)) || (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
    }
}

プロパティの上に属性をドロップします。

于 2010-11-04T14:36:02.123 に答える