none
find if a record has no relation with records in other tables ? RRS feed

  • Question

  • Hello !

    before deletion of a record , i want to know if this record has no relation with records on other tables through relationship.

    the database is sql server 2008r2 , vb.net 2010 , using entity framework 4.1

    is there an easy way?

    tabel1           table2                  table3

    id1                id2                       id3

    name            value1                  value2

                        id1                        id1

    so , if i want to delete a record from table 1 (id1=1 , name=name1) how to find if on other tables has no record related with this ? there is a property or should i use a query ?

     

    thank you.

    Wednesday, August 31, 2011 1:54 PM

Answers

  • Hi dcode25;

    The table1 table should have navigation properties to the other two table and therefore should look like this :

    tabel1
    id1  
    name 
    table2s
    table3s

    table2s and table3s are collections properties and can be queried for a count. So to see if the other tables have related records you can do the following :

    var query = from t1 in context.Table1
          where t1.table2s.Count() == 0 && t1.table3s.Count() == 0
          select t1;
    

    the variable query will have a collection of those t1 records that have no related records to table2 and table3.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by dcode25 Wednesday, August 31, 2011 10:33 PM
    Wednesday, August 31, 2011 4:05 PM

All replies

  • Hi dcode25;

    The table1 table should have navigation properties to the other two table and therefore should look like this :

    tabel1
    id1  
    name 
    table2s
    table3s

    table2s and table3s are collections properties and can be queried for a count. So to see if the other tables have related records you can do the following :

    var query = from t1 in context.Table1
          where t1.table2s.Count() == 0 && t1.table3s.Count() == 0
          select t1;
    

    the variable query will have a collection of those t1 records that have no related records to table2 and table3.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by dcode25 Wednesday, August 31, 2011 10:33 PM
    Wednesday, August 31, 2011 4:05 PM
  • Thank you !
    Wednesday, August 31, 2011 10:34 PM
  •  

    Not a problem, glad to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, August 31, 2011 11:49 PM