Fluent nhibernate - Many to Many mapping with attribute?

Your domain model does not seem to match your database model - the Resource class has the property AccessLevel (i.e. One AccessLevel per Resource) but in the DB model AccessLevel is a column on the map table (i.e. One AccessLevel per User-Resource relation).

Your domain model does not seem to match your database model - the Resource class has the property AccessLevel (i.e. One AccessLevel per Resource) but in the DB model AccessLevel is a column on the map table (i.e. One AccessLevel per User-Resource relation).

Assuming the DB model is the correct model one (fairly straightforward) way of mapping this would be to introduce a class like this. Public class UserResource { public virtual int UserResourceId { get; protected set; } public virtual User User { get; set; } public virtual Resource { get; set; } public virtual string AccessLevel { get; set; } } and map it in this way: public class UserResourceMap : ClassMap { public UserResourceMap() { Table("UserResourceMap"); Id(x => x. UserResourceId); References(x => x.

User). UniqueKey("UniqueUserAndResource"); References(x => x. Resource).

UniqueKey("UniqueUserAndResource"); Map(x => x. AccessLevel); } } If you want bidirectional associations you could also add a Collection property on User and/or Resource and map these with HasMany(...).Inverse(). Of course, this kind of mapping would introduce a new UserResourceId column in the UserResourceMap table (using a composite key consisting of User and Resource would mitigate that).

Another solution would be to add an EntityMap association. If the association is owned by User it would be a Dictionary property. Something like this might do the trick: public class User { public virtual int UserId { get; protected set; } public virtual string Name { get; set; } public virtual Dictionary Resources { get; set; } // Resource -> AccessLevel } public class UserMap : ClassMap { public UserMap() { Table("User"); Id(x => x.

UserId); Map(x => x. Name); HasMany(x => x. Resources).AsEntityMap().

Element("AccessLevel"); } }.

Sorry, missed Ian's answer - but the second part of my answer contains something new at least. – Yhrn Feb 17 at 15:04 I'll upvote yours if you'll upvote mine... :-) – Ian Nelson Feb 17 at 15:13 Ah...THe second part was wat I have been looking for..! – Mulki Feb 17 at 15:39 . AsEntityMap Says its depracated...and also Element doesn't exist in it.

– Mulki Feb 17 at 16:23 @Mulki: I have checked it now and I have a mapping exactly like that and AsEntityMap() is not obsolete and Element() do exist and is used to specify the column name of the for the Dictionary values in the mappings table. Works like a charm. I use NH3 and the "stable pre-release for NH3" version of Fluent but I'm pretty sure I had the same mapping earlier before I switched from NH2.1.2/Fluent1.1.– Yhrn 1.1.8 at 9:31.

As you've correctly identified in your database schema, this isn't a pure many-to-many relationship - it's two one-to-many relationships as the intermediate table has an attribute (the access level). I therefore think your domain is missing an entity - there doesn't appear to be any relationship in your model between a user and the resources they can access. How about something like this: public class User { public virtual int Id { get;protected set; } public virtual string Name { get;set; } public virtual ICollection UserResources { get; set;} } public class UserResource { public virtual int Id { get; protected set; } public virtual User User { get; set;} public virtual Resource Resource { get; set;} public virtual string AccessLevel { get; set;} } public class Resource { public virtual int Id { get;protected set; } public virtual string Name { get;set; } } And mappings like: public class UserMap : ClassMap { public UserMap() { Id(x => x.Id); Map(x => x.

Name); HasMany(x => x. UserResource) .AsSet() .Inverse() .Cascade.AllDeleteOrphan(); } } public class UserResourceMap : ClassMap { public UserResourceMap() { Table("UserResourceMap"); Id(x => x.Id); References(x => x. User).Not.Nullable(); References(x => x.

Resource).Not.Nullable(); Map(x => x. AccessLevel); } } public class ResourceMap : ClassMap { public ResourceMap() { Cache.ReadOnly(); Id(x => x. Id); Map(x => x.

Name); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions