开发者

Nhibernate update problem - Parameter index is out of range

开发者 https://www.devze.com 2023-03-31 13:29 出处:网络
I am having problem with updating data to database. I have a simple many-to-one Product - Group relationship:

I am having problem with updating data to database. I have a simple many-to-one Product - Group relationship:

    public class GroupInfoMapping : ClassMap<GroupInfo>
{
    public GroupInfoMapping()
    {
        Table("`group`");
        Schema("tempdb");
        Id(x => x.Id)
            .GeneratedBy.Native();
        OptimisticLock.Version();
        Map(x => x.GroupName);
      开发者_StackOverflow社区  Map(x => x.ParentId);
                       HasMany(x => x.Products)
                           .KeyColumn("GroupId")
                           .Inverse()
                           .Fetch.Select()
                           .AsSet();
                       //HasMany(x => x.Children)
                       //    .KeyColumn("Id")
                       //    .Inverse()
                       //    .Fetch.Select()
                       //    .Cascade.None()
                       //    .AsSet();
                       //References(x => x.Parent)
                       //    .Class(typeof(GroupInfo))
                       //    .Column("ParentId")
                       //    .Fetch.Select();

    }
}

     public class ProductInfoMapping : ClassMap<ProductInfo>
{
    public ProductInfoMapping()
    {
        Table("`product`");
        Schema("tempdb");
        Id(x => x.Id)
            .GeneratedBy.Native();
        OptimisticLock.Version();
                       Map(x => x.GroupId);
        Map(x => x.ProductName);
                       References(x => x.Group)
                         .Class(typeof(GroupInfo))
                         .Column("GroupId")
                         .Fetch.Select();
    }
}

There is no difference if I change only the product data, it will not allow me to update with exception: Parameter index is out of range.

If I add Not.Update() to Product Mapping for Group reference, I am able to update product data (but Group remains unreferenced).

I am using MySql database and I am using one session per presenter. Why I cannot update both entity and its reference? I have commented out part for selfreferening the group, since this also creates problem when updating group only.

Thanks, Goran


In your ProductInfoMapping class, you are mapping the same column twice:

Map(x => x.GroupId);

and

References(x => x.Group)
    .Class(typeof(GroupInfo))
    .Column("GroupId")
    .Fetch.Select();

The Map of the group id is unneeded as you can access the id of the Group property without it:

var groupId = someProdInfoObj.Group.Id;

Update:

If you still desire that there be a GroupId property which you can access the id of the Group instead of accessing it directly on the Group object, you can just modify the property like so (example is for an id with a Guid type):

public virtual Guid? GroupId
{
    get { return Group != null ? Group.Id : null; }
    set { }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号