Is there a way to delete all foreign entities when i delete a record using NHibernate?
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,开发者_JAVA技巧 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!
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.
精彩评论