views:

25

answers:

1

I recently began using Fluent Nhibernate for my data layer and have come across an issue. Whenever i want to delete a record that has multiple foreign key constraints, i have to create another class just to represent that database entity. That means that for something like a User record, which has relationships with many other tables, i have to create something like 10 different classes that i will never use for any other purpose. At least that is my understanding of how things work.

Is there a way for me to delete all of these records without having to map them. For instance, using the User example, a User can have multiple roles, departments, email addresses, phone numbers, addresses, and so on. I would like to delete all of these records, but not have to map all of them in Nhibernate classes.

Is there a property i can set on my UserMapping that would accomplish this?

Thanks!

+1  A: 

If those entities aren't mapped, NHibernate can't possibly know about them. But you could pick one of these alternatives:

  • Set up FK cascades at the database level (if your database supports it)
  • Use a database trigger to manually code the cascade (if your database supports triggers)
  • Use a IPostDeleteEventListener (similar to a database trigger but at NHibernate level) to manually code the cascade.
Mauricio Scheffer
Thanks for the response. I was really hoping there would some kind of under-the-hood magic that would read the schema and cascade the delete where necessary, but yes i can see that being unrealistic. I guess what i will have to do is create the necessary classes and only map the properties that i have to.
bsayegh

related questions