Reputation: 9157
I've just started to play around with Fluent NHibernate and would like some help / pointers on how I got about mapping this. Here's the domain model I'm working with:
Class User: Your standard user class
Class Preference: A set of key value pairs. A user can have many preferences.
Class Post: Omitted complete description for the sake of brevity, but a user creates a post and it can contain one or more preferences.
So, to recap:
class User
{
IList<Preference> Preferences;
IList<Post> Posts;
}
class Post
{
IList<Preference> PostData;
}
Hibernate automapper comes up with the following DB Structure:
User
------------------------
User_Id
Preference
------------------------
Preference_Id | User_Id | Post_Id
Post
------------------------
Post_Id | User_Id
The preference table looks ugly to me and would much rather have something like:
User
------------------------
User_Id
Preference
------------------------
Preference_Id | User_Id
Post
------------------------
Post_Id | User_Id | Preference_Id
Any help on how to do this is much appreciated! I already have a class that implements the IAutoMappingOverride interface, but I'm not sure how to do the actual mapping.
Thanks,
Teja
Upvotes: 0
Views: 427
Reputation: 86
I would reference this configuration to get you manual and auto mappings configured correctly:
Mixing Mixing Automapping and manual mapping
I also noticed you have some composite keys in both your preference and post tables I would look into learning how to map composite keys. Here is a link that might help you with mapping composite keys(primary key with more than one column):
Using Fluent NHibernate to map Composite Keys
As far as your fluent maps go the following might get you pointed in the right direction you can map one-to-many
relationships using both HasMany
or References
depending on which table you want the foreign key on:
public class UserMap : ClassMap<User>
{
public UserMap(){
Id(x => x.Id).Column("User_Id").GeneratedBy.Identity();
HasMany(x => x.Preferences);
}
}
public class PostMap: ClassMap<Post>
{
public UserMap(){
Id(x => x.Id).Column("Post_Id").GeneratedBy.Identity();
References(x => x.Preferences);
}
}
Upvotes: 1
Reputation: 30813
since the preferences are the same a simple property is enough
class Post
{
public virtual User User { get; set; }
public virtual IList<Preference> Preferences { get { return User.Preferences; } }
}
Upvotes: 0