Make (Fluent) NHibernate Recreate only specific tables in a database - nhibernate

Is there a way to tell NHibernate to leave some tables alone? I have the ASPNET Membership tables, and while I would like to be able to access them using NHibernate, I don't want SchemaExport to delete them and recreate them while recreating the other tables. (Goodness knows what may happen)

You need to use SchemaAction.None()
More information here: http://www.lostechies.com/blogs/rodpaddock/archive/2010/06/29/using-fluent-nhibernate-with-legacy-databases.aspx

Related

Associate ASP.NET ApplicationUser In Another Database

I have an ASP.NET MVC 5 web app. I have connected to a database on localDB ("MyWebAppDatabase"), which already contains many tables. I am accessing this using ADO.NET Entity Framework, and this is all working great.
However, I would like to add a table which references users who use the website: I have a "Subscription" table and would like to associate it with an ApplicationUser. The problem is that the tables containing user information are stored in a separate database (which was automatically generated by the Visual Studio when I created the project under the DefaultConnection context), and I don't know how I can perform this association.
What is the best way to go about this? I thought the ideal solution would be if I could move the tables that ASP.NET automatically created for application users into MyWebAppDatabase - then I can easily update the database with the correct tables and foreign keys. Is this correct? If so, how would I go about doing this? I'm not entirely sure where the database is for the application users (I couldn't decipher it from looking at Web.Config and reading the DefaultConnection connection string) and I don't really understand how I would be able to migrate the tables.
Thank you all for your help!
Ideally if you can move the tables into a single database you will get the best performance, otherwise you will have to do all of the JOIN's in memory in the application. You can't make foreign key references across database unfortunately.
If you point the connection string for the ASP.NET Identity to the same database that your Subscription table is located in and run the application and create some users it should create those tables automatically.

Foreign Keys cascading in JPA or database Schema

I have a separate script that creates the database and tables for each database that we are supporting. I am using JPA to manipulate the data in the database, but JPA does not create the database or the tables.
I want to add a foreign key to a new table with a cascade property so that when a row is deleted in the parent table, the corresponding rows in the child table are also deleted.
I am aware of the annotations necessary to do this in JPA, however I can create the foreign keys and the cascade statements in the script I am using to create the databases.
My question is, since I am using a separate script to create the database tables, can I just add the foreign key / cascade statements in the script and then ignore all of the JPA relationship annotations? Is there advantages/disadvantages to adding this information in both the database script as well as in the JPA code?
You should always have a 2 level check. if you do not use the features of JPA, then it's a big waste of the functionality JPA provides. you should actually make sure that you JPA relations match your DB relations as closely as possible. It will help you a lot as JPA can cache data and even prevent unnecessary calls to DB.
eg if u have a not null constraint and you persist with no JPA constraint, your DB has to do all the work and throw the exception back.
normally in an application, the network and DB are the slowest factors in the app. so you should try mimicking the constraints in JPA to avoid unnecessary overhead.
also using such constraints you can form bidirectional relationships and have collections of associated entities and many more such advantages.

Multiple database with NHibernate 3.x

I found a couple of articles how to use NHibernate with multiple database, for example this one
http://codebetter.com/karlseguin/2009/03/30/using-nhibernate-with-multiple-databases/
But all articles are very old, and may be there is some new approach with NH 3.x? I looked in documentation but did not found anything, but maybe i missed somthing?
Does anybody knows some better way (native NH3.x way) to use NH 3.x with multiple database than described in this article?
http://codebetter.com/karlseguin/2009/03/30/using-nhibernate-with-multiple-databases/
Thanks,
Alexander.
AFAIK, there is nothing new in NH 3. But there are still more options to use several databases than in the blog post you linked.
You can open your own connection and pass it to NH when opening a session.
You can open a session and switch to another database on the same server (eg. by executing a use database statement on sql server).
You can provide a schema (database) name on each table you map in the mapping file. It is not useful to have it hard coded, but you can still replace it after loading the mapping files or use mapping by code.
The articles you linked are still the way to go. Each SessionFactory is responsible for a single connection (connectionstring) and schema.
There is one special case where ou split the database into multiple with the same schema to load balance. This is called sharding and there is the contrib NHibernate.Shards to deal with it.

nhibernate update schema : relationships not removed

im using nhibernate (version 3.1.0) with ms compact edition 4.0 and have a 'many to one' relationship for some tables.
works fine, constraints are checked correctly.
i've changed the relationship by adding 'not-found="ignore"' in the hbm.xml, so that i will not have an error when the parent needs a child.
the database is updated by using 'NHibernate.Tool.hbm2ddl.SchemaUpdate', but the relationship is still there ?!
Anyone an idea how to remove these relationships in an update?
thanks.
By default SchemaUpdate will do everything so that you don't lose your data. Deleting something from your schema may have such an impact. So NHibernate won't do that.
But you can do SchemaExport to recreate all tables with constraints.

NHibernate - Generate Domain from Database

I know its possible to generate the database tables from the domain model. But is there any way of doing things the other way. I have a totally awful database (worst I have ever seen). Its sharded (16 Shards!!), split across multiple postgres databases (all on the same server) with foreign key relations like urn:dbtable:guid.
Its proving a major pain in the ass to migrate using SSIS so I want to use NHibernate, read the data into objects and rewrite to a SQL Server database in blissful data-architectural harmony.
Is there any way to scan the current DB using NH or other and build a domain model and mappings?
Thanks!
NHibernate Mapping Generator
- A simple utility to generate NHibernate mapping files and corresponding domain classes from existing DB tables
It's free.