开发者

Fluent Nhibernate and Dynamic Table Name

开发者 https://www.devze.com 2022-12-20 21:53 出处:网络
I\'ve got a parent and child object. Depending on a value in the parent object changes the table for the child object. So for example if the parent object had a reference \"01\" then it will look in t

I've got a parent and child object. Depending on a value in the parent object changes the table for the child object. So for example if the parent object had a reference "01" then it will look in the following table "Ch开发者_高级运维ild01" whereas if the reference was "02" then it would look in the table "Child02". All the child tables are the same as in number of columns/names/etc.

My question is that how can I tell Fluent Nhibernate or nhibernate which table to look at as each parent object is unique and can reference a number of different child tables?

I've looked at the IClassConvention in Fluent but this seems to only be called when the session is created rather than each time an object is created.


I found only two methods to do this.

  1. Close and recreate the nhibernate session every time another dynamic table needs to be looked at. On creating the session use IClassConvention to dynamically calculate the name based on user data. I found this very intensive as its a large database and a costly operation to create the session every time.
  2. Use POCO object for these tables with custom data access.

As statichippo stated I could use a basechild object and have multiple child object. Due to the database size and the number of dynamic table this wasn't really a valid option.

Neither of my two solutions I was particularly happy with but the POCO's seemed the best way for my problem.


NHibernate is intended to be an object relational mappers. It sounds like you're doing more of a scripting style and hoping to map your data instead of working in an OOP manner.

It sounds like you have the makings of an class hierarchy though. What it sounds like you're trying to create in your code (and then map accordingly) is a hierarchy of different kinds of children:

BaseChild

--> SmartChild

--> DumbChild

Each child is either smart or dumb, but since they all have a FirstName, LastName, Age, etc, they all are instances of the BaseChild class which defines these. The only differences might be that the SmartChild has an IQ and the DumbChild has a FavoriteFootballTeam (this is just an example, no offense to anyone of course ;).

NHibernate will let you map this sort of relationship in many ways. There could be 1 table that encompasses all classes or (what it sounds like you want in your case), one table per class.

Did I understand the issue/what you're looking for?

0

精彩评论

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

关注公众号