Profile

Cover photo
Thomas Farrell
105 followers|304,155 views
AboutPostsPhotosYouTubeReviews

Stream

 
This morning my coworker and I diagnosed a case of Entity Framework 4 returning the wrong data from a view. This was not a case of “we messed up how we handle the data” or “we query the data incorrectly”, we actually found that EF was simply returning genuinely wrong data.

When you add a view to Entity Framework, it selects one or more columns that it considers the “entity key”, which determines uniqueness of the row. (How it selects them is a mystery.) If the selected columns are the same for more than one row, EF simply returns a copy of the first row matching that set, even if the rows aren’t actually the same. Thus, I had three rows that were different, but because the columns EF had selected as the “entity key” columns matched, it showed me the same results for all three rows. (Debugging proved that as I iterated over the result set, it returned the same instance to me three times.)

In my particular case, every row of the results should be considered unique. However, there was no one column which could be used as a unique entity key. There was combination of two column which, together, define uniqueness, but when I tried to use them as the entity key EF threw errors, because one of them sometimes has null values. To get around this I added a fake column to my SQL which I defined as “newId() AS rowid”. I tried making that the entity key but EF threw additional errors. I correctly guessed that perhaps EF had simply gotten too screwed up about my view for the problem to be resolved directly, so I dropped and re-added the view from EF designer (we use database-first), and this resolved the problem.

So, in summary, beware results from views in EF, they may be false if you don’t carefully check the entity key(s).
1
Daniel Reyes's profile photoSam Jongenelen's profile photoTony Berno's profile photoThomas Farrell's profile photo
9 comments
 
+Richard Deeming Yes, you're quite right that it relates to the caching behavior, and what it was doing was exactly what you speculate. It was problematic because EF selected several columns as its key which were, in fact, not the key. I wish I had some clue how/why it selected them.

As this was found as a bug in a production system, I already applied the workaround I described, put it through testing, and got it into production so the software would provide correct data. However, thanks for suggesting .AsNoTracking() , I'll try that in the future. 
Add a comment...
 
Kansas intends to just break laws while they fight to take away the civil rights of their citizens. #KansasTheBigotState  
Even though same-sex couples can legally marry in the state, some state agencies refuse to offer them the same services as heterosexual couples while the state appeals in court.
2
1
Joseph Teller's profile photo
 
SpaceShipTwo has apparently been destroyed, with fatalities. Terrible news.
1

Thomas Farrell

Shared publicly  - 
 
"Video: GOP congressman says Ferguson protesters of 'a single continental origin'" http://feedly.com/e/6PAn8NS4
Speaking on conservative internet TV show NewsMax, Rep. Steve King said that he was not concerned that police in Ferguson, Missouri were racially profiling protesters, because all of ...
1

Thomas Farrell

Shared publicly  - 
 
"Video: GOP congressman says Ferguson protesters of 'a single continental origin'" http://feedly.com/e/6PAn8NS4
Speaking on conservative internet TV show NewsMax, Rep. Steve King said that he was not concerned that police in Ferguson, Missouri were racially profiling protesters, because all of ...
1
Alexander Skrabut's profile photoThomas Farrell's profile photo
2 comments
 
I just think it's interesting that he feels so comfortable to be so openly racist.

Thomas Farrell

Shared publicly  - 
 
(sigh) It has been a hell of a day.

Once, many moons ago, in the dark ages - 1994 - I was walking on Arlington St in Boston on a frozen winter morning, past the Ritz Carlton. Newbury Street was deserted, except for one woman standing in front of the Ritz. She was elderly, but even from 50 feet away, radiant with elegance and effortless sophistication, in a full length fur, gloved hand holding the collar closed against the bitter wind. She was so elegant I had to pause to gawk for a moment, thinking "wow, that woman is so unbelievably classy, she looks just like Lauren Bacall!" Except of course that I knew that Lauren Bacall was dead.

Except, I learned later, that she wasn't dead, not by far. She was in Boston to be photographed for the Milk Mustache campaign, and staying at the Ritz. I'm sad I didn't know, and a bit sadder that I didn't think to just walk over and tell her she was beautiful anyway. Because she was, breathtakingly beautiful, even at age 70, even to a gay man.
Lauren Bacall, the great actor who became best known as Humphrey Bogart's on and offscreen partner, has died. She was 89, and died Tuesday morning of a stroke at her longtime home at the Dakota, a famous building in New Yor...
6

Thomas Farrell

Shared publicly  - 
 
I'd heard of this, but the article was really interesting. 
Over the course of six weeks during the height of the Cold War, almost three million Soviets visited an exhibition that celebrated America. American kitchens, American art, American cars, and most especially American capitalism. The American National Exhibition in Moscow was a full-court press to convince the Soviet people of American superiority.
1

Thomas Farrell

Shared publicly  - 
 
Thanks to a new TSA rule, If your phone battery is dead or your laptop can't hold a charge, you're out of luck.
2
Have him in circles
105 people
Lars Colson's profile photo
Tim Wilson's profile photo
Chris Patti's profile photo
Kenneth Haase's profile photo
Peter Adams's profile photo
Keith Rodwell's profile photo
shuli natan's profile photo
AJK-Web's profile photo
David Meleedy's profile photo

Communities

6 communities

Thomas Farrell

Shared publicly  - 
 
Laws in all 50 states permitting people to carry concealed firearms in public have been connected to a rise in violent crimes, according to a new report from researchers at Stanford and Johns Hopkins universities. The report, published in Septembe...
1
Thomas Farrell's profile photoAlexander Skrabut's profile photo
2 comments
 
You mean an increase in availability to a weapon that requires little training and little thought to use would actually increase undesired wanton violence and crime?! The next thing you're going to tell me is that denying a human a thing makes them want that thing even more and they'll commit crimes to get it if they want it bad enough.

#humannature
 
Boston Public Library courtyard
3

Thomas Farrell

Shared publicly  - 
1

Thomas Farrell

Shared publicly  - 
1
People
Have him in circles
105 people
Lars Colson's profile photo
Tim Wilson's profile photo
Chris Patti's profile photo
Kenneth Haase's profile photo
Peter Adams's profile photo
Keith Rodwell's profile photo
shuli natan's profile photo
AJK-Web's profile photo
David Meleedy's profile photo
Communities
6 communities
Links
Contributor to
Work
Occupation
Software Engineer and Manager
Basic Information
Gender
Male
The pizza is excellent, when they get your order right. I've had an awful lot of occasions when they don't.
Public - 4 months ago
reviewed 4 months ago
2 reviews
Map
Map
Map