ヌルではないフィールドの流暢NHibernateはAutomapの大会
-
25-09-2019 - |
質問
いくつかのいずれかのヘルプには、どのように私はのための非NULLを持つように自動マップ命令するだろう コラム?
public class Paper : Entity
{
public Paper() { }
[DomainSignature]
[NotNull, NotEmpty]
public virtual string ReferenceNumber { get; set; }
[NotNull]
public virtual Int32 SessionWeek { get; set; }
}
はしかし、私は次のように取得しています
<column name="SessionWeek"/>
私はそれが流暢なマップを使用して行うことができます知っています。しかし、私はそれを知っていただきたいと思います 自動マッピングの道ます。
解決
ありがとうございます。また、参照プロパティのReferenceConventionの必要性のために行われます。これは動作するコードです。
public class ColumnNullConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
if (instance.Property.MemberInfo.IsDefined(typeof(NotNullAttribute), false))
instance.Not.Nullable();
}
} public class ReferenceConvention : IReferenceConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
{
instance.Column(instance.Property.Name + "Fk");
if (instance.Property.MemberInfo.IsDefined(typeof(NotNullAttribute), false))
instance.Not.Nullable();
}
}
他のヒント
ここで基本的にあなたがコードで参照リンクから取られ、私はそれを行う方法です。いくつかの他の有用な規則があるにもあります。
HTH、
Berryl
/// <summary>
/// If nullability for the column has not been specified explicitly to allow NULL, then set to “NOT NULL”.
/// </summary>
/// <remarks>see http://marcinobel.com/index.php/fluent-nhibernate-conventions-examples/</remarks>
public class ColumnNullabilityConvention : IPropertyConvention, IPropertyConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Nullable, Is.Not.Set);
}
public void Apply(IPropertyInstance instance)
{
instance.Not.Nullable();
}
}
あなたは自動マッピングの結果とほとんど幸せ時折、私はそれを達成するための最も簡単な方法は、そのクラスのためIAutoMappingOverrideを実装見つけるクラスのプロパティのいくつかを言うためにそれをオーバーライドする必要がある場合:
public class UserMappingOverride : IAutoMappingOverride<User>
{
public void Override(AutoMapping<User> mapping)
{
mapping.Map(x => x.UserName).Column("User").Length(100).Not.Nullable();
}
}
そして、このようにそれらを使用します:
AutoMap.AssemblyOf<User>().UseOverridesFromAssemblyOf<UserMappingOverride>();
クラスマップと同様に- しかし、あなたは、クラス内のすべてのフィールドを記述する必要はありません。 このアプローチは、Entity Frameworkのコードファースト流暢のAPIの方法に非常によく似てます。
public class Paper Map : IAutoMappingOverride<Paper >
{
public void Override(AutoMapping<Paper> mapping)
{
mapping.Map(x => x.ReferenceNumber).Not.Nullable();
}
}
のInt32は、デフォルトではNULL可能タイプではありません。 INT32?あなただけのInt32として指定することにより、非NULL可能にするので、NULL可能です。
あなたはこれを自動的に行うために規則を使用することができます。私が使用する規則を確認していないが、正しいものを見つけるためFluentNHibernate.Conventions.Instancesを見てみましょう。それはこのように見ていきます。
public class ColumnConvention : IColumnConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.ColumnInstance instance)
{
if (instance.EntityType.IsDefined(typeof(NotNullAttribute), false))
instance.NotNull = true;
}
public void Apply(FluentNHibernate.Conventions.Instances.IColumnInstance instance)
{
return;
}
}
ちょうどあなたの自動マッピングにこの規則を追加します。
私は少なからず見つける私はこの規則を作成し、唯一のnullとして列を指定することを好むので、私の列は、nullではありません。
/// <summary>
/// Indicates that a column should allow nulls
/// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Property)]
public class NullableAttribute : Attribute
{
}
public class ColumnIsNotNullByDefaultConvention : IPropertyConvention, IPropertyConventionAcceptance
{
public void Apply(IPropertyInstance instance)
{
instance.Not.Nullable();
}
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(c => !c.Property.MemberInfo.IsDefined(typeof(NullableAttribute), false));
}
}