Self-Referencing many to many issue

ยอดดู 97 ครั้ง
ข้ามไปที่ข้อความที่ยังไม่อ่านรายการแรก

Kasper22

ยังไม่อ่าน,
18 ก.ย. 2552 13:07:2518/9/52
ถึง Fluent NHibernate
Hi,

I'm trying to upgrade a project with the release of Fluent Nhibernate
from an old unknown version and I've run into a problem. I've been
digging at it for a while and hit a wall, I just don't see how some
values are being set so I'm having no luck moving further into this
issue. Anyway to the point.

First a disclaimer I didn't write this code, and I'm thinking there is
a better way to do this; so if anyone see a better way feel free to
point that out.

The problem seems to be an issue with the AutoMapper and the
HasManyToMany overrides.

Here is the simple version of what I have:

There is a use class, with all the normal fields and normal fun
stuff. This is the little group of properties Fluent NHibernate is
getting confused with:

public virtual IList<TblUser> SentFriendRequests { get; set; }
public virtual IList<TblUser> ReceivedFriendRequests { get; set; }
public virtual IList<TblUser> Friends { get; set; }
public virtual IList<TblUser> FriendOf { get; set; }

the overrides:

mapping.HasManyToMany(x => x.Friends)
.Table("TblFriendPivot")
.ParentKeyColumn("FkUser")
.ChildKeyColumn("FkFriendUser");

mapping.HasManyToMany(x => x.FriendOf)
.Table("TblFriendPivot")
.ParentKeyColumn("FkFriendUser")
.ChildKeyColumn("FkUser")
.Inverse();

mapping.HasManyToMany(x => x.SentFriendRequests)
.Table("TblFriendRequestPivot")
.ParentKeyColumn("FkUser")
.ChildKeyColumn("FkRequestUser");

mapping.HasManyToMany(x => x.ReceivedFriendRequests)
.Table("TblFriendRequestPivot")
.ParentKeyColumn("FkRequestUser")
.ChildKeyColumn("FkUser")
.Inverse();

When it runs I end up getting an exception "Can't figure out what the
other side of a many-to-many should be." from the
BiDirectionalManyToManyPairingVisitor class. When I debug into that
class there is a variable relationships that I can't figure out where
it gets set, but that guy only seems to show the first two options in
the mapping overrides.

Thanks in advance.
Bryan

Kasper22

ยังไม่อ่าน,
21 ก.ย. 2552 11:03:2421/9/52
ถึง Fluent NHibernate
Hey All,

Well I got a little rest this weekend and dug at the problem a little
more today. It looks like the problem is that if you have more than
one self-referencing relationship in the same table things get messed
up. All I ended up needing to do was add a check to see if the tables
the relationship were supposed to tie to had the same name and now it
is working. So it looks like this in the
BiDirectionalManyToManyPairingVisitor where the last line there is
what I added.

var potentialOtherSides = relationships
.Where(x => x.ContainingEntityType ==
mapping.ChildType && x.ChildType == mapping.ContainingEntityType
&& !string.IsNullOrEmpty(x.TableName) && x.TableName.Equals
(mapping.TableName, StringComparison.Ordinal));

I'm sure James has a fancier way of fixing this issue, but this looks
like it will work for me right now.

Bryan

Paul Batum

ยังไม่อ่าน,
25 ก.ย. 2552 10:51:5825/9/52
ถึง fluent-n...@googlegroups.com
Thanks for posting your solution Bryan, I'll get a test written that replicates this problem and see about integrating your fix.
ตอบทุกคน
ตอบกลับผู้สร้าง
ส่งต่อ
ข้อความใหม่ 0 รายการ