2011-05-27 214 views
0

我收到以下錯誤:功能NHibernate映射幫助

無效指數3本OracleParameterCollection以計數=當我試圖插入一個新列到表3

我的實體類是:

public class Operator 
{ 
    public Operator() 
    { 
     this.OperatorMemberships = new List<OperatorMembership>(); 
    } 
    public virtual int Id { get; set; } 
    public virtual int OperatorMembershipId { get; set; } 
    public virtual int SchemeId { get; set; } 
    public virtual string Description { get; set; } 
    public virtual IList<OperatorMembership> OperatorMemberships { get; set; } 
} 

public class OperatorMembership 
{ 
    public virtual int Id { get; set; } 
    public virtual int OperatorId { get; set; } // delete 
    public virtual int UserRoleId { get; set; } // delete 
    public virtual UserRole UserRole { get; set; } 
    public virtual Operator Operator { get; set; } 
} 

我的映射是:

public OperatorMap() 
    { 
     Table("OPERATOR"); 
     Id(x => x.Id, "OPERATOR_ID").GeneratedBy.Identity(); 
     Map(x => x.SchemeId, "SCHEME_ID"); 
     Map(x => x.Description, "DESCRIPTION"); 
     //HasMany(x => x.OperatorMemberships).Inverse().AsBag().Cascade.AllDeleteOrphan(); 
     HasMany(x => x.OperatorMemberships).Inverse().AsBag(); 
     //References(x => x.SchemeServices, "OPERATOR_ID"); 
    } 
} 

public class OperatorMembershipMap : ClassMap<OperatorMembership> 
{ 
    public OperatorMembershipMap() 
    { 
     Table("USER_OPERATOR_MEMBERSHIP"); 
     Id(x => x.Id, "USER_OPERATOR_MEMBERSHIP_ID").GeneratedBy.Identity(); 
     Map(x => x.OperatorId, "OPERATOR_ID"); 
     Map(x => x.UserRoleId, "USER_ROLE_ID"); 

     References(x => x.Operator).Column("OPERATOR_ID"); ; 
     References(x => x.UserRole).Column("USER_ROLE_ID"); 
    } 
} 

代碼插入新行:

public int AddOperatorMembership(int operatorId, int userRoleId) 
    { 
     OperatorMembership op = new OperatorMembership(); 
     op.OperatorId = operatorId; 
     op.UserRoleId = userRoleId; 
     _provider.AddEntity(op); 

     return op.Id; 
    } 

和我的數據庫表:

USER_OPERATOR_MEMBERSHIP_ID -- OPERATOR_ID -- USER_ROLE_ID 

任何幫助都很好,我確信有一些非常聰明流利的nhibernate開發人員可以馬上發現錯誤。

非常感謝。

Randolf。

回答

1

你映射的引用的實體鍵和外鍵:

public OperatorMembershipMap() 
{ 
    Table("USER_OPERATOR_MEMBERSHIP"); 
    Id(x => x.Id, "USER_OPERATOR_MEMBERSHIP_ID").GeneratedBy.Identity(); 
    // Map(x => x.OperatorId, "OPERATOR_ID"); 
    // Map(x => x.UserRoleId, "USER_ROLE_ID"); 

    References(x => x.Operator).Column("OPERATOR_ID"); ; 
    References(x => x.UserRole).Column("USER_ROLE_ID"); 
} 

您也可以從實體中刪除外鍵:

public class OperatorMembership 
{ 
    public virtual int Id { get; set; } 
    // public virtual int OperatorId { get; set; } // delete 
    // public virtual int UserRoleId { get; set; } // delete 
    public virtual UserRole UserRole { get; set; } 
    public virtual Operator Operator { get; set; } 
} 
+0

如果刪除的屬性,然後我不能與我的代碼訪問它們用於與這些值的更新:op.OperatorId = operatorId; op.UserRoleId = userRoleId; – 2011-05-27 13:33:36

+0

好的,但是你會在你的問題中出現錯誤。 – mathieu 2011-05-27 14:54:45

+0

你可以使用op.Operator = session.Load(operatorId) – mathieu 2011-05-27 14:56:14

1

我也跨越了同樣的問題來了,結束了用google搜索半天,但沒有成功。 Mathieu給出的上述答案似乎相關,我也嘗試過,但對我沒有幫助。然後,我嘗試刪除引用並保持屬性完好無損,它爲我工作,希望也能爲你工作。

Table("USER_OPERATOR_MEMBERSHIP"); 
    Id(x => x.Id, "USER_OPERATOR_MEMBERSHIP_ID").GeneratedBy.Identity(); 
    Map(x => x.OperatorId, "OPERATOR_ID"); 
    Map(x => x.UserRoleId, "USER_ROLE_ID"); 

    //References(x => x.Operator).Column("OPERATOR_ID"); ; 
    //References(x => x.UserRole).Column("USER_ROLE_ID"); 

從類取出相同以及

public class OperatorMembership 
{ 
    public virtual int Id { get; set; } 
    public virtual int OperatorId { get; set; } // delete 
    public virtual int UserRoleId { get; set; } // delete 
    //public virtual UserRole UserRole { get; set; } 
    //public virtual Operator Operator { get; set; } 
}