Fluent NHibernate - Delete one entity by Id somehow deleted ALL entities in the DB

985 views
Skip to first unread message

Dev Enthusiast

unread,
Mar 7, 2014, 12:30:13 AM3/7/14
to fluent-n...@googlegroups.com
I'm new to Fluent NHibernate.  I followed the example at https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started

The example showed how to Create and Read entities.  At run-time, it creates 2 stores.

I added a DeleteByStoreId() method to this sample code.

        public static void DeleteByStoreId(int StoreId)
        {
            var sessionFactory = CreateSessionFactory();

            using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var store = session.Get<Store>(StoreId);

                    if (store != null)
                    {
                        session.Delete(store);
                    }

                    transaction.Commit();
                }
            }
        }

When I execute DeleteByStoreId by supplying one of the two existing StoreId, it somehow deletes BOTH stores.  I would have expected it to delete ONLY the one store with the given StoreId.  Can you tell me what I'm doing wrong?

I have to prove to my boss that Fluent NHibernate is capable of doing the CRUD operations so that I get the go ahead to use it in my new project.  Hope you can answer me as soon as possible.

Thank you!

Dev Enthusiast

unread,
Mar 7, 2014, 5:35:49 PM3/7/14
to fluent-n...@googlegroups.com
More information.  I used SQL Profiler to monitor the SQL calls.  The example program (https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started) created 2 Store_id's as follows:

Store_id = 105 and = 106

After executing DeleteByStoreId(105), this is the sequence of SQL calls that took place:

2014-03-06 21:41:44.7276|DEBUG|NHibernate.SQL|
     ********************************************************************************
     SELECT store0_.Id as Id10_0_, store0_.Name as Name10_0_ FROM [Store] store0_ WHERE store0_.Id=@p0;
@p0 = 105 [Type: Int32 (0)]
     ********************************************************************************

     SELECT products0_.Store_id as Store2_1_, products0_.Product_id as Product1_1_, product1_.Id as Id8_0_, product1_.Name as Name8_0_, product1_.Price as Price8_0_, product1_.Aisle as Aisle8_0_, product1_.Shelf as Shelf8_0_ FROM StoreProduct products0_ left outer join [Product] product1_ on products0_.Product_id=product1_.Id WHERE products0_.Store_id=@p0;
@p0 = 105 [Type: Int32 (0)]
     ********************************************************************************

     SELECT storesstoc0_.Product_id as Product1_1_, storesstoc0_.Store_id as Store2_1_, store1_.Id as Id10_0_, store1_.Name as Name10_0_ FROM StoreProduct storesstoc0_ left outer join [Store] store1_ on storesstoc0_.Store_id=store1_.Id WHERE storesstoc0_.Product_id=@p0;
@p0 = 307 [Type: Int32 (0)]
     ********************************************************************************

     SELECT storesstoc0_.Product_id as Product1_1_, storesstoc0_.Store_id as Store2_1_, store1_.Id as Id10_0_, store1_.Name as Name10_0_ FROM StoreProduct storesstoc0_ left outer join [Store] store1_ on storesstoc0_.Store_id=store1_.Id WHERE storesstoc0_.Product_id=@p0;
@p0 = 308 [Type: Int32 (0)]
     ********************************************************************************

     SELECT storesstoc0_.Product_id as Product1_1_, storesstoc0_.Store_id as Store2_1_, store1_.Id as Id10_0_, store1_.Name as Name10_0_ FROM StoreProduct storesstoc0_ left outer join [Store] store1_ on storesstoc0_.Store_id=store1_.Id WHERE storesstoc0_.Product_id=@p0;
@p0 = 309 [Type: Int32 (0)]
     ********************************************************************************

     SELECT storesstoc0_.Product_id as Product1_1_, storesstoc0_.Store_id as Store2_1_, store1_.Id as Id10_0_, store1_.Name as Name10_0_ FROM StoreProduct storesstoc0_ left outer join [Store] store1_ on storesstoc0_.Store_id=store1_.Id WHERE storesstoc0_.Product_id=@p0;
@p0 = 310 [Type: Int32 (0)]
     ********************************************************************************

     SELECT products0_.Store_id as Store2_1_, products0_.Product_id as Product1_1_, product1_.Id as Id8_0_, product1_.Name as Name8_0_, product1_.Price as Price8_0_, product1_.Aisle as Aisle8_0_, product1_.Shelf as Shelf8_0_ FROM StoreProduct products0_ left outer join [Product] product1_ on products0_.Product_id=product1_.Id WHERE products0_.Store_id=@p0;
@p0 = 106 [Type: Int32 (0)]
     ********************************************************************************

     SELECT storesstoc0_.Product_id as Product1_1_, storesstoc0_.Store_id as Store2_1_, store1_.Id as Id10_0_, store1_.Name as Name10_0_ FROM StoreProduct storesstoc0_ left outer join [Store] store1_ on storesstoc0_.Store_id=store1_.Id WHERE storesstoc0_.Product_id=@p0;
@p0 = 311 [Type: Int32 (0)]
     ********************************************************************************

     SELECT storesstoc0_.Product_id as Product1_1_, storesstoc0_.Store_id as Store2_1_, store1_.Id as Id10_0_, store1_.Name as Name10_0_ FROM StoreProduct storesstoc0_ left outer join [Store] store1_ on storesstoc0_.Store_id=store1_.Id WHERE storesstoc0_.Product_id=@p0;
@p0 = 312 [Type: Int32 (0)]
     ********************************************************************************

     SELECT staff0_.Store_id as Store4_1_, staff0_.Id as Id1_, staff0_.Id as Id7_0_, staff0_.FirstName as FirstName7_0_, staff0_.LastName as LastName7_0_, staff0_.Store_id as Store4_7_0_ FROM [Employee] staff0_ WHERE staff0_.Store_id=@p0;
@p0 = 106 [Type: Int32 (0)]
     ********************************************************************************

     SELECT staff0_.Store_id as Store4_1_, staff0_.Id as Id1_, staff0_.Id as Id7_0_, staff0_.FirstName as FirstName7_0_, staff0_.LastName as LastName7_0_, staff0_.Store_id as Store4_7_0_ FROM [Employee] staff0_ WHERE staff0_.Store_id=@p0;
@p0 = 105 [Type: Int32 (0)]
     ********************************************************************************

     DELETE FROM StoreProduct WHERE Store_id = @p0;
@p0 = 105 [Type: Int32 (0)]
     ********************************************************************************

     DELETE FROM StoreProduct WHERE Store_id = @p0;
@p0 = 106 [Type: Int32 (0)]
     ********************************************************************************

     Batch commands:
command 0:DELETE FROM [Product] WHERE Id = @p0;
@p0 = 307 [Type: Int32 (0)]
command 1:DELETE FROM [Product] WHERE Id = @p0;
@p0 = 308 [Type: Int32 (0)]
command 2:DELETE FROM [Product] WHERE Id = @p0;
@p0 = 309 [Type: Int32 (0)]
command 3:DELETE FROM [Product] WHERE Id = @p0;
@p0 = 311 [Type: Int32 (0)]
command 4:DELETE FROM [Product] WHERE Id = @p0;
@p0 = 312 [Type: Int32 (0)]

     ********************************************************************************

     Batch commands:
command 0:DELETE FROM [Employee] WHERE Id = @p0;
@p0 = 256 [Type: Int32 (0)]
command 1:DELETE FROM [Employee] WHERE Id = @p0;
@p0 = 257 [Type: Int32 (0)]

     ********************************************************************************

     Batch commands:
command 0:DELETE FROM [Store] WHERE Id = @p0;
@p0 = 106 [Type: Int32 (0)]

     ********************************************************************************

     Batch commands:
command 0:DELETE FROM [Product] WHERE Id = @p0;
@p0 = 310 [Type: Int32 (0)]

     ********************************************************************************

     Batch commands:
command 0:DELETE FROM [Employee] WHERE Id = @p0;
@p0 = 258 [Type: Int32 (0)]
command 1:DELETE FROM [Employee] WHERE Id = @p0;
@p0 = 259 [Type: Int32 (0)]
command 2:DELETE FROM [Employee] WHERE Id = @p0;
@p0 = 260 [Type: Int32 (0)]

     ********************************************************************************

     Batch commands:
command 0:DELETE FROM [Store] WHERE Id = @p0;
@p0 = 105 [Type: Int32 (0)]

     ********************************************************************************

When I put a breakpoint in DeleteByStoreId(), I can see that 

var store = session.Get<Store>(StoreId);

only returns the element of Store_Id=105.  So, why would it go and delete Store_id=106 as well??

Thanks.

Chris Bingham

unread,
Mar 7, 2014, 6:42:10 PM3/7/14
to fluent-n...@googlegroups.com

Sounds like a cascade is wrong somewhere...

I notice that procuct.stores stocked in is cascade.all that seems pretty wrong to me (suspect it means if I delete a product it will also delete the store)

Hth, Chris

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fluent-nhibern...@googlegroups.com.
To post to this group, send email to fluent-n...@googlegroups.com.
Visit this group at http://groups.google.com/group/fluent-nhibernate.
For more options, visit https://groups.google.com/d/optout.

Oskar Berggren

unread,
Mar 11, 2014, 4:11:20 AM3/11/14
to fluent-n...@googlegroups.com
I would concur that the cascading rules are too liberal. Judging from the text in the article it sounds like a lazy way to add an object graph and only having to call Save() on a single "starting point".

I find that when it comes to cascading it's good to think about object ownership and lifetime. Does A own B? Is the lifetime of B controlled by (the lifetime of) A? Is to, let add/delete cascade from A to B (but not in the other direction).

/Oskar
Reply all
Reply to author
Forward
0 new messages