流畅的NHibernate映射帮助

时间:2011-05-27 08:59:02

标签: fluent-nhibernate nhibernate-mapping fluent

我收到以下错误:

此OracleParameterCollection的索引3无效,Count = 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开发人员可以立即发现错误。

非常感谢。

兰道夫。

2 个答案:

答案 0 :(得分: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; }
}

答案 1 :(得分:1)

我也遇到了同样的问题,结果谷歌搜索了半天但没有成功。 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; }
}