After add all mappings and before SchemaExport/BuildSessionFactory you can
iterate NH's metadata and auto-add database-objects.
var mappings =
configuration.CreateMappings(Dialect.Dialect.GetDialect(configuration.Properties));
foreach (var table in mappings.IterateTables)
{
foreach(var fk in table.ForeignKeyIterator)
{
IAuxiliaryDatabaseObject auxdo = CreateAlterFk(fk );
mappings.AddAuxiliaryDatabaseObject()
}
}

On Thu, Aug 5, 2010 at 12:33 PM, v64 <[email protected]> wrote:

> Fabio,
>
> Thanks for your quick reply.
>
> I've tried to do this through a custom dialect and ran into a problem
> because of the way NHibernate builds the constraint string.
>
> ForeignKey.SqlCreateString() adds "alter table <table_name> ", then
> calls
> ForeignKey.SqlConstraintString() which appends the output from
> Dialect.GetAddForeignKeyConstraintString(), normally something like
> "add constraint [blah] foreign key ([blah]) references [blah]"
> after that ForeignKey.SqlConstraintString()  tacks on " on delete
> cascade" if cascade delete is enabled.
>
> What I need to generate is two separate commands for each foreign
> key.
>
> ALTER TABLE [pf].[Invoice] WITH NOCHECK ADD CONSTRAINT
> [FKDC60817A07C258D] FOREIGN KEY ([BillTo]) REFERENCES [pf].
> [OrderContact] NOT FOR REPLICATION
> ALTER TABLE [pf].[Invoice] NOCHECK CONSTRAINT [FKDC60817A07C258D]
>
> I could output "WITH NOCHECK ADD CONSTRAINT ..." in
> Dialect.GetAddForeignKeyConstraintString() and then tack on the second
> ALTER to it (which is kind of a hack in itself). But now if cascade
> delete is enabled, ForeignKey.SqlConstraintString()  will add "on
> delete cascade", and that will break it.
>
> Any ideas?
>
> TIA
> v
>
> On Aug 5, 9:44 am, Fabio Maulo <[email protected]> wrote:
> > On Wed, Aug 4, 2010 at 6:48 PM, Jason Dentler <[email protected]
> >wrote:
> >
> > > You can probably create a custom dialect to do this. I'm not sure if
> > > there's an easier way.
> >
> > Yes Jason, that is the easy way.
> > Method
> > GetAddForeignKeyConstraintString
> > --
> > Fabio Maulo
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>


-- 
Fabio Maulo

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to