c# - Setting up one to many relationship with Fluent Nhibernate -


i'm having bit of argument co-worker can't seem find answer yet basic stuff.

establishing one-to-many relationship in fluent nhibernate entity.

let's take roles , users example. role can assigned multiple users made entity guts looks like:

public class user {     [required]     public virtual string firstname { get; set; }     public virtual role role { get; set; } } 

and role

public class role {     [required]     public virtual string name { get; set; }     public virtual ilist<user> users{ get; set; }      public role()     {         users = new list<users>();     } } 

as can see i'm referencing collection of users in roles, kind of saying each role have multiple users. user entity has role entity reference needed identify role user belong to.

in opinion correct way link , co-worker says having role reference users create circular reference. who's right ?

i tried finding answer online. think question tells me i'm right: fluent nhibernate many 1 mapping

but looked @ fuent nhibernate sample project here https://github.com/jagregory/fluent-nhibernate/tree/master/src/examples.firstautomappedproject/entities , don't example of i'm trying implement. can guys advise or me find document explaining correct way ? right? thank you.

what proposing here entirely possible , allowable within nhibernate framework. have listed models rather mapping files fluent nhibernate allows configure mappings in way without issue.

whether choose map relationship in way entirely down personal preference , specific scenario. have mapped models in way equally have chosen not to, because in instances doesn't make sense overly complicate object graph. take example look-up table (e.g. culture or locale) referenced many times across multiple tables in database, when mapping have culture property in each 1 of parent models not have collections of parent objects in culture model - doesn't make sense.

you need consider loading data through persistence layer - if create sort of relationship , need simple list of roles need consider when, or if , users collection populated - can specify eager or late loading in experience specifying eager loading corresponding fetch command in queries can result in more optimized call database.

basically saying there no absolute "correct way" when deciding how define mappings - need balance rich object model vs. query performance, specific example acceptable if need it.

i have re-read question , not sure if asking examples of mappings configured in way if want examples let me know , i'll put you.

to achieve relationship describe need following map classes (i have added id properties guess have those):

for roles:

public class rolemap : classmap<role> {     public rolemap()     {           table(@"roles");           id(x => x.id).generatedby.assigned();           map(x => x.name).column("name");           hasmany<user>(x => x.users)             .inverse()             .keycolumns.add("roleid", mapping => mapping.name("roleid"));     } } 

for users:

public class usermap : classmap<user> {    public usermap()     {           table(@"users");           id(x => x.id).generatedby.assigned();           map(x => x.firstname);           map(x => x.roleid);               references(x => x.role)             .class<role>()             .columns("roleid");     } } 

i wouldn't hung on "your method vs method" - above acceptable, , depends on requirements when come use object model in code. alternatively if don't want users collection in role map remove property , associated hasmany mapping declaration. 1 thing note above .inverse() specification has delegated management of relationship between role , user users entity, makes sense process of adding or editing existing user , providing roleid forge relationship.

hope helps if have more specific questions let me know


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -