none
How can I wire loaded (or cached) related entities together? RRS feed

  • Question

  • Hi everyone,

    I am a newbie to this LINQ to SQL and I have a requirement to solve but I don't know how. I created 2 entities named Account and Contact. A Contact has a foreign key to Account named AccountId. I use the New LINQ to SQL Entities Wizard to create the necessary entity classes. After the wizard, I have two classes named Account and Contact. And the Contact class has a property named Account refer to Account class. 

    In my application, I have two views manipulating each entity class mentioned above. And my application has a application-defined security policy that an end-user only sees the Account or Contact that he created or was assigned by another end-user in the application. This leads to the case that user U1 creates a account named A and a contact named C referring to this account A. Then user U1 assigns the contact C to user U2. So the user U2 can see the contact C but cannot see the account A. The code I use to load accounts and contacts like following:

    if (CanShowAllAccounts)

    LoadAllAccounts();

    else

    LoadAccountsByOwner(CurrentIdentityName);

    if (CanShowAllContacts)

    LoadAllContacts();

    else

    LoadContactsByOwner(CurrentIdentityName);

    After this line of code, I set a breakpoint and I get the result that the Account property of all the Contacts (that is generated by the LINQ to SQL wizard) always is null. I turned off the DeferredLoadingEnabled of the DataContext because of the security policy mentioned above. So how can I solve this case? In the document of Microsoft, LINQ to SQL is said to be able to track object changes and identities. But I cannot understand why even if I load all the accounts (by calling my LoadAllAccounts), the Account property of the Contact object is still not set to the loaded entity. Or the LINQ does not support it? Or do I have to write codes to wire those related entities together?

    Thanks for attention. For more information, I develop my application on .NET Framework 3.5 SP1 and MS Visual Studio 2008 SP1.

    Best regards,

    Sherlock1984

    Tuesday, January 25, 2011 2:08 AM

Answers


  • Hi Sherlock1984,

    Welcome to linq to sql forums.
    As your description, I realized that you want to prevert one who have no right against the data.
    As far as i know when you set the defferloadingenabled with false, the deffered data will be ignored.
    DB

     

     

    The defferloadingenable with false:
     
               DataClassesDataContext test = new DataClassesDataContext();
               test.DeferredLoadingEnabled = false;
               var students = test.AStudents.ToList();
               foreach (AStudent s in students)
               {
                   Console.WriteLine(s.AClass.ClassName);
               }
    The query builded by linq:

     SELECT [t0].[ClassID], [t0].[ClassName] 
     FROM [dbo].[Class] AS [t0] 
     WHERE [t0].[ClassID] = @p0 

    You will find that the query have't aginst students information from database.
    I suggest you as the follows:

                DataClassesDataContext test = new DataClassesDataContext();
     
                var assoloadoption = new DataLoadOptions();
                assoloadoption.AssociateWith<AClass>(c => c.AStudents.Where(s=>s.StudentID==1));
                test.LoadOptions = assoloadoption;
              
                var vclass = test.AClasses.First();
                foreach (AStudent s in vclass.AStudents)
                {
                    Console.WriteLine(s.Name);
                }

    The associatewith could used for filters the objects retrieved for a particular relationship.
    Additionly, there is the description of

    defferloadingenable
    http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.deferredloadingenabled.aspx

    associatewith

    http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.associatewith.aspx

    I hope that coule be userful for you.
    Regards!

    David Peng

     

    Wednesday, January 26, 2011 10:36 AM
    Moderator