Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
4.50/5 (4 votes)
See more:
I use Linq To Entities to get 2 objects m1 & m2.
And I don't understand why 2 different objects reference the SAME Template table.

I suspect that the reason due to the connection between MConfigOnPage1, MConfigOnPage2 with MConfiguration. Maybe it should be splitted somehow?

Here is attached my ERD and the code.

I'll be grateful for explanation why this happens?

Thank you

var cxt = new Entities();
//this returns MConfiguration with Id=19
var m1 = (from mop in cxt.MConfigOnPage1
          where mop.SiteMapId == 15 && mop.HolderId == 13
          select mop.MConfiguration).FirstOrDefault();
//this returns MConfiguration with Id=40
var m2 = (from mop in cxt.MConfigOnPage2
          where mop.SiteMapId == 15 && mop.HolderId == 1
          select mop.MConfiguration).FirstOrDefault();

var t1 = m1.Holder.Template;
var t1.Code = 13;
var t2 = m2.Holder.Template;
//I expect that t2.Code to be 0, but it equals 13
//This behavior tells me that m1 & m2 reference the same Template object,
//   BUT shouldn't m1 & m2 to have their own Template objects?
Posted
Updated 9-Mar-11 0:22am
v2

1 solution

SQL
Link to Entities ensures, within a given context, if you fetch the same entity ( by primary key in DB) you will get the same object.

You will see this same behavior is you selected the row from the template table multiple times. You will always get the same instance back whenever you re-query for any object.

This gives you performance advantages through caching and prevents multiple edits to the same object in the same context from causing conflicts.
 
Share this answer
 
Comments
TheAteist 15-Mar-11 18:48pm    
but m1 & m2 retrieved from different tables. You want to say that even m1 & m2 retrieved from DIFFERENT tables, if they have 2 DIFFERENT objects BUT with the same PRIMARY KEY this is considered as identical objects??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900