2012-06-18 130 views
1

我有兩個例子使用Fluent NHibernate映射。流利的NHibernate映射是評論,我試圖做基於代碼映射,但仍然出現ArgumentNullException「值不能爲空」。如何使它正確?如何將流利的NHibernate映射到NHibernate內置的基於代碼映射

//Fluent NHIbernate mapping for table LocalizationEntry 
//public class LocalizationEntryMapping : ClassMap<LocalizationEntry> 
//{ 
// public LocalizationEntryMapping() 
// { 
//  Cache.ReadWrite(); 
//  CompositeId() 
//   .ComponentCompositeIdentifier(x => x.Id) 
//   .KeyProperty(x => x.Id.Culture) 
//   .KeyProperty(x => x.Id.EntityId) 
//   .KeyProperty(x => x.Id.Property) 
//   .KeyProperty(x => x.Id.Type); 
//  Map(x => x.Message); 
// } 
//} 
public class LocalizationEntryId 
{ 
    public virtual string Culture { get; set; } 
    public virtual string Type { get; set; } 
    public virtual string Property { get; set; } 
    public virtual string EntityId { get; set; } 

    public override bool Equals(object obj) 
    { 
     if (obj != null) 
     { 
      LocalizationEntryId other = obj as LocalizationEntryId; 
      if (other != null) 
      { 
       return this.Type  == other.Type && 
         this.Property == other.Property && 
         this.EntityId == other.EntityId && 
         this.Culture == other.Culture; 
      } 
     } 
     return false; 
    } 

    public override int GetHashCode() 
    { 
     return base.GetHashCode(); 
    } 
} 

public class LocalizationEntry : IDomainMapper 
{ 
    public virtual LocalizationEntryId Id { get; set; } 
    public virtual string Message { get; set; } 

    public virtual void Map(ModelMapper mapper) 
    { 
     mapper.Class<LocalizationEntry>(m => 
     {     
      m.ComposedId(t => 
      { 
       t.Property(g => g.Id.Culture, c => 
       { 
        c.NotNullable(true); 
        c.Length(10); 
       }); 
       t.Property(g => g.Id.EntityId, c => 
       { 
        c.NotNullable(true); 
       }); 
       t.Property(g => g.Id.Property, c => 
       { 
        c.NotNullable(true); 
        c.Length(100); 
       }); 
       t.Property(g => g.Id.Type, c => 
       { 
        c.NotNullable(true); 
        c.Length(100); 
       }); 
      }); 

      m.Property(t => t.Message, c => 
      { 
       c.NotNullable(true); 
       c.Length(400); 
      }); 
     }); 
    } 
} 

////////////編輯//////////////// 我找到了解決辦法了。映射應該這樣完成:

public virtual void Map(ModelMapper mapper) 
     { 
      mapper.Class<LocalizationEntry>(m => 
      { 
       m.ComponentAsId(x => x.Id, n => 
       { 
        n.Property(x => x.Culture); 
        n.Property(x => x.EntityId); 
        n.Property(x => x.Property); 
        n.Property(x => x.Type); 
       }); 

       m.Property(t => t.Message, c => 
       { 
        c.NotNullable(true); 
        c.Length(400); 
       }); 
      }); 
     } 
+0

爲什麼不使用'ClassMapping '? –

+0

如果您找到了解決方案,請將其自己發佈爲答案並接受。這樣,它比在同一個問題中編輯更清晰。謝謝 –

回答

1

我找到了解決方案。映射應該這樣完成:

public virtual void Map(ModelMapper mapper) 
    { 
     mapper.Class<LocalizationEntry>(m => 
     { 
      m.ComponentAsId(x => x.Id, n => 
      { 
       n.Property(x => x.Culture); 
       n.Property(x => x.EntityId); 
       n.Property(x => x.Property); 
       n.Property(x => x.Type); 
      }); 

      m.Property(t => t.Message, c => 
      { 
       c.NotNullable(true); 
       c.Length(400); 
      }); 
     }); 
    }