2011-11-07 5 views
1

Fluent NHibernate Automapper Overrideを使用して、エンティティのユニークなカラムを指定しようとしています。 CodeTypeのテストクラスでは、Typeプロパティを一意にしたいと思います。目標は、現在保存されているCodeTypeと同じタイプフィールドを使用して作成された新しい "CodeType()"が現在のエンティティの上にオーバーレイされることです。Fluent NHibernate Automap Overrideで一意のカラムを定義する

I持って、次のCODETYPEクラス:

public class CodeType : SecurableEntity 
{ 

    public virtual string Type { get; set; } 
    public virtual string Description { get; set; } 

    /// <summary> 
    /// This is a placeholder constructor for NHibernate. 
    /// A no-argument constructor must be available for NHibernate to create the object. 
    /// </summary> 
    public CodeType() { } 


} 

I以下CodeTypeMapクラスがあります。

public AutoPersistenceModel Generate() 
    { 
     var mappings = AutoMap.AssemblyOf<User>(new AutomappingConfiguration()); 
     mappings.IgnoreBase<Entity>(); 
     mappings.IgnoreBase<SecurableEntity>(); 
     mappings.IgnoreBase(typeof(EntityWithTypedId<>)); 
     mappings.Conventions.Setup(GetConventions()); 
     mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>(); 
     mappings.UseOverridesFromAssemblyOf<UserMap>(); 
     mappings.UseOverridesFromAssemblyOf<CodeMap>(); 
     mappings.UseOverridesFromAssemblyOf<CodeTypeMap>(); 

     return mappings; 
    } 
:オーバーライドは、以下を通じて、自動マップに適用される

public class CodeTypeMap : IAutoMappingOverride<CodeType> 
{ 
    public void Override(AutoMapping<CodeType> mapping) 
    { 
     //Doesn't work. Need a way to specify a column as unique. 
     mapping.Map(m => m.Type).Unique(); 
    } 
} 

"type"が "existingTyp"の既存のレコードを更新するには、次のコードを使用しますe "である。

SecurableEntityRepository<CodeType> ctr = new SecurableEntityRepository<CodeType>(); 
CodeType ct = new CodeType(); 
ct.type = "existingType"; 
ct = ctr.SaveOrUpdate(ct); 

どのようにタイプフィールドのNHibernateキーを一意にすることができますか?

これは可能ですか?

+0

を言っているの削除またはprotected CodeType() { }行うことができます"? –

+0

また、私はこれらすべてのmappings.UseOverridesFromAssemblyOf (); 'が必要であるとは確信していません。すべてのオーバーライドが同じアセンブリ内に存在する場合は、それらのうちの1つだけが必要です。 –

答えて

0

短い答えですが、あなたが望むものは非常に多くの可能性があるため、コードで扱わなければならないものです。毎回あなたがすでに1

SecurableEntityRepository<CodeType> ctr = new SecurableEntityRepository<CodeType>(); 
CodeType ct = ctr.GetByType("existingType"); 
if (ct == null) 
{ 
    ct = new CodeType { type = "existingType" }; 
} 
ctr.SaveOrUpdate(ct); 

または

SecurableEntityRepository<CodeType> ctr = new SecurableEntityRepository<CodeType>(); 
CodeType ct = ctr.GetByType("existingType"); 
if (ct != null) 
{ 
    ctr.Detach(ct); 
    ctr.Merge(new CodeType{ type = "existingType" }); 
} 

または

SecurableEntityRepository<CodeType> ctr = new SecurableEntityRepository<CodeType>(); 
int ctId = ctr.GetIdByType("existingType"); 
if (ct != 0) 
{ 
    ctr.Merge(new CodeType{ Id = ctId, type = "existingType" }); 
} 

がある場合はデシベルをチェックする必要が新しい CodeTypeを作成して、別々に書き込むことができますいくつかのものがあります

  • 「更新CODETYPEセットタイプ= 『existingType』タイプ= 『型』:

    public CodeType() { }は、ドメインのために必要でない場合、あなたはこのような何かをしたい

  • public AutoPersistenceModel Generate() 
    { 
        return AutoMap.AssemblyOf<User>(new AutomappingConfiguration()) 
         .IgnoreBase<Entity>() 
         .IgnoreBase<SecurableEntity>() 
         .IgnoreBase(typeof(EntityWithTypedId<>)) 
         .Conventions.Setup(GetConventions()) 
         .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>(); 
    }