none
how to check an entity is an reference entity in entity framework RRS feed

  • Question

  • Hi ,

    I am working on  MVC3 with Entity Framework .

    I am trying to delete the record in EMP entity (table),but  this EMP Id is foriegn key in more than 3 entieties (tables),

    how to check if this EMP Id existed in any other entities. suppose we don't know the other entity name,how to get all other entities which are having foreign key reference of this EMP.

    Please help me on this issue.


    DeviSree

    Thursday, November 22, 2012 9:46 AM

Answers

  • Hi DeviSree;

    The code snippet below will return a Emp record for a specific Emp and return one Emp record with associated table results or null if not found and iterates through the collections.

    var eName = "Emp record to look for";
    
    // Create a DbContext to query the database
    var ctx = new DbContextEntities();
    
    // Query the database for the Emp record to delete
    var emp = (from emp in ctx.Emp.Include("EmpAddresses").Include("EmpDepartments").Include("EmpSalaries")
               where emp.EmpName == eName
               select emp).SingleOrDefault();
                   
    if( emp != null )
    {
        // emp holds a single Emp table record
        
        // Iterate over the collections to find which record belong to this Emp reord
        if( emp.EmpAddresses.Count() > 0 )
        {
        		foreach( var eAdd in emp.EmpAddresses )
        		{
        				// Access EmpAddress records for the Emp record here
        		}    		
        }
        
        // Do in like maner in the other two collections
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    Friday, November 23, 2012 3:25 PM

All replies

  • You could try to get ObjectStateEntry associated with this record and use RelationshipManager to get list of IRelatedEnd.

    var changeRecord = context.ObjectStateManager.GetObjectStateEntry(obj);
    var relations = changeRecord.RelationshipManager.GetAllRelatedEnds();
    Thursday, November 22, 2012 12:20 PM
  • Hi Devisree;

    The EMP table has navigation properties to the other tables it has a relationship with. If you iterate over those navigation properties it will return to you the records in that table that has a relationship with the record in EMP.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, November 22, 2012 2:17 PM
  • Thanks Fernando,

    Could you pleas provide a sample code for this,

    It would be a great help to me.

    Thanks,

    devisree.


    DeviSree

    Friday, November 23, 2012 5:38 AM
  •   

    In order for me to show some sample code I will need to see the schema of all the table that will be used in the query and there relationships. Posting an image from the edmx designer of the table will also do.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, November 23, 2012 6:43 AM
  • Hi,

    Please check the see below image of EDMX.


    DeviSree


    • Edited by Devisree Friday, November 23, 2012 7:19 AM
    Friday, November 23, 2012 7:17 AM
  • Hi DeviSree;

    The code snippet below will return a Emp record for a specific Emp and return one Emp record with associated table results or null if not found and iterates through the collections.

    var eName = "Emp record to look for";
    
    // Create a DbContext to query the database
    var ctx = new DbContextEntities();
    
    // Query the database for the Emp record to delete
    var emp = (from emp in ctx.Emp.Include("EmpAddresses").Include("EmpDepartments").Include("EmpSalaries")
               where emp.EmpName == eName
               select emp).SingleOrDefault();
                   
    if( emp != null )
    {
        // emp holds a single Emp table record
        
        // Iterate over the collections to find which record belong to this Emp reord
        if( emp.EmpAddresses.Count() > 0 )
        {
        		foreach( var eAdd in emp.EmpAddresses )
        		{
        				// Access EmpAddress records for the Emp record here
        		}    		
        }
        
        // Do in like maner in the other two collections
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    Friday, November 23, 2012 3:25 PM