【问题标题】:NHibernate - Invalid index for this SqlParameterCollection with Count (Mapping By Code)NHibernate - 此 SqlParameterCollection 的无效索引与计数(按代码映射)
【发布时间】:2013-11-17 20:36:31
【问题描述】:

我在使用 NHibernate 代码映射和我的一个类中的复合键时遇到了真正的问题。

Domain类如下:-

    public partial class UserRole {
    public virtual int UserId { get; set; }
    public virtual int RoleId { get; set; }
    //public virtual UserRoleId Id { get; set; }
    public virtual User User { get; set; }
    public virtual Role Role { get; set; }
    [NotNullNotEmpty]
    public virtual DateTime VqsCreateDate { get; set; }
    public virtual DateTime? VqsUpdateDate { get; set; }
    [NotNullNotEmpty]
    public virtual bool VqsMarkedForDelete { get; set; }
    [Length(50)]
    public virtual string VqsCreateUserName { get; set; }
    [Length(50)]
    public virtual string VqsLastUpdatedUserName { get; set; }
    [NotNullNotEmpty]
    [Length(128)]
    public virtual string VqsCreateSessionId { get; set; }
    [Length(128)]
    public virtual string VqsLastUpdatedSessionId { get; set; }
    [NotNullNotEmpty]
    [Length(15)]
    public virtual string VqsCreateIpAddress { get; set; }
    [Length(15)]
    public virtual string VqsLastUpdatedIpAddress { get; set; }
    [Length(255)]
    public virtual string VqsCreateSessionInfo { get; set; }
    [Length(255)]
    public virtual string VqsLastUpdatedSessionInfo { get; set; }
    [NotNullNotEmpty]
    public virtual bool VqsAllowDelete { get; set; }
    public virtual bool? VqsAllowEdit { get; set; }
    [Length(50)]
    public virtual string VqsRffu1 { get; set; }
    [Length(50)]
    public virtual string VqsRffu2 { get; set; }
    [Length(50)]
    public virtual string VqsRffu3 { get; set; }
    #region NHibernate Composite Key Requirements
    public override bool Equals(object obj) {
        if (obj == null) return false;
        var t = obj as UserRole;
        if (t == null) return false;
        if (UserId == t.UserId
         && RoleId == t.RoleId)
            return true;

        return false;
    }
    public override int GetHashCode() {
        int hash = GetType().GetHashCode();
        hash = (hash * 397) ^ UserId.GetHashCode();
        hash = (hash * 397) ^ RoleId.GetHashCode();

        return hash;
    }
    #endregion
}

而映射类如下:-

   public partial class UserRoleMap : ClassMapping<UserRole> {

    public UserRoleMap() {
        Schema("Membership");
        Lazy(true);
        ComposedId(compId =>
            {
                compId.Property(x => x.UserId, m => m.Column("UserId"));
                compId.Property(x => x.RoleId, m => m.Column("RoleId"));
            });
        Property(x => x.VqsCreateDate, map => map.NotNullable(true));
        Property(x => x.VqsUpdateDate);
        Property(x => x.VqsMarkedForDelete, map => map.NotNullable(true));
        Property(x => x.VqsCreateUserName, map => map.Length(50));
        Property(x => x.VqsLastUpdatedUserName, map => map.Length(50));
        Property(x => x.VqsCreateSessionId, map => { map.NotNullable(true); map.Length(128); });
        Property(x => x.VqsLastUpdatedSessionId, map => map.Length(128));
        Property(x => x.VqsCreateIpAddress, map => { map.NotNullable(true); map.Length(15); });
        Property(x => x.VqsLastUpdatedIpAddress, map => map.Length(15));
        Property(x => x.VqsCreateSessionInfo, map => map.Length(255));
        Property(x => x.VqsLastUpdatedSessionInfo, map => map.Length(255));
        Property(x => x.VqsAllowDelete, map => map.NotNullable(true));
        Property(x => x.VqsAllowEdit);
        Property(x => x.VqsRffu1, map => map.Length(50));
        Property(x => x.VqsRffu2, map => map.Length(50));
        Property(x => x.VqsRffu3, map => map.Length(50));
        ManyToOne(x => x.User, map =>
        {
            map.Column("UserId");
            ////map.PropertyRef("Id");
            map.Cascade(Cascade.None);
        });

        ManyToOne(x => x.Role, map =>
        {
            map.Column("RoleId");
            ////map.PropertyRef("Id");
            map.Cascade(Cascade.None);
        });

    }
}

每当我尝试插入此表时,我都会收到以下错误:-

此 SqlParameterCollection 的索引无效,计数为“N”

我已经查看了 StackExchange 和互联网上最后一天出现的所有此错误,但仍然没有进一步说明。

似乎有很多关于 Xml 和 Fluent 映射的示例,但对于 Mapping By Code 的例子却很少。

我想我已经尝试了各种建议组合,但都无济于事。

我完全意识到问题与我在表中的 ID 字段被引用两次有关,这最终导致了错误,

问题是我的类中同时需要 ID 和实体字段,因为它们在整个代码中被广泛使用。

我似乎遇到了这个问题,因为我的多对多表中有复合键和外键的组合。

任何人都可以提供任何帮助来保持我的理智,我们将不胜感激。

非常感谢。

西蒙

【问题讨论】:

    标签: nhibernate


    【解决方案1】:

    您可以设置 Insert(false)Update(false) 以防止 Nhibernate 生成包含 User 和 Role 列(不存在)的更新或插入语句。

            ManyToOne(x => x.User, map =>
            {
                map.Column("UserId");
                ////map.PropertyRef("Id");
                map.Cascade(Cascade.None);
                map.Insert(false);
                map.Update(false);
            });
    
            ManyToOne(x => x.Role, map =>
            {
                map.Column("RoleId");
                ////map.PropertyRef("Id");
                map.Cascade(Cascade.None);
                map.Insert(false);
                map.Update(false);
            });
    

    这与 Fluent 映射中的 Not.Update() Not.Insert() 相同。

    如果您现在创建一个有效的UserRole 对象并将机器人 ID 设置为有效的用户/角色 ID,NHibernate 应该能够持久化您的对象。

    【讨论】:

    • 非常感谢。有时,我花了很多时间在看某样东西,我就是只见树木不见森林。这是我第一次在 StackExchange 上发帖,不敢相信你回复的速度有多快。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    相关资源
    最近更新 更多