2014-12-07 27 views
0

當通過代碼功能使用NHibernate的映射時,如何將實體的Id映射到專用的支持字段?如何將Id映射到NHibernate的代碼映射中的私有支持字段?

public abstract class Entity : IEntity 
{ 
    private Guid? _id; 

    protected Entity() { } 

    protected Entity(Guid? id) 
    { 
     _id = id; 
    } 

    #region IEntity Members 

    /// <summary> 
    /// Gets the unique id for this entity. 
    /// </summary> 
    /// <value>The id.</value> 
    public Guid? Id 
    { 
     get { return _id; 
    } 
} 

映射:

public abstract class GuidKeyedClassMapping<T> : ClassMapping<T> where T : class, IEntity 
{ 
    protected GuidKeyedClassMapping() 
    { 
     // What to write here??? 
     Id(x=> x.Id); 
    } 
} 

試圖與指出的屬性或字段與字符串,但無濟於事。

Id(x => "_id", m => m.Access(Accessor.Field)); 

...給我:

類型的異常「System.Exception的」發生在NHibernate.dll但 不是在用戶代碼的其他信息處理:無效 表達式類型:預期ExpressionType.MemberAccess,發現恆

回答

1

Id(x => x.Id, m => m.Access(Accessor.Field));應該工作,因爲_id比賽LowerCaseUnderscoreStrategy。注意x.Id必須在您的第一個代碼中指定

相關問題