How to map existing tables into a inheritance hierarchy in NHibernate?
The problem is trying to map inheritance given an existing table structure. The table is also used by legacy apps using raw sql, ie, the table can't delete any existing schema details, but can add more to it.
The existing table is already mapped, and essentially has a bunch of fields with the following problem....
so existing class
class A
{
// Id etc
public virtual Client Client { get; set; }
}
with a table structure like
table A (
I开发者_Python百科d INT IDENTITY NOT NULL,
Client_id INT null,
primary key (Id)
)
Now I want to introduce a base class
class Base
{
// Id etc
public virtual Client Client { get; set; }
}
which will end up with a table like...
table Base (
Id INT IDENTITY NOT NULL,
Client_id INT null,
primary key (Id)
)
and change A to
class A : Base
{
// clients moved to the base...
}
which will do something like this to the table :-
table A(
Base_id INT IDENTITY NOT NULL,
Id INT not null, // I will need to keep the existing Id field...
Client_id INT null, // this Client now conflicts with the Base client
primary key (Base_id)
)
I'm using table per class inheritance.
the problem is....
'Client' will be on the table of "Base" and also it's on the existing table of "A". I want to be able to keep using the 'Client' on "A", as a kind of override.
How do I do this? can I do this?
or is it possible that on table "Base" it doesn't have 'Client' at all and then on all the subclass tables they define "Client"? (which would make querying interesting)
if its only for querying NH should support Querying by interface
class A : IHasClient
{
// Id etc
public virtual Client Client { get; set; }
}
class SomeOther : IHasClient
{
// Id etc
public virtual Client Client { get; set; }
}
var classesWithClients = session.QueryOver<IHasClient>().List();
精彩评论