18607

Entity Framework is not returning the proper data. It is repeating data from a single record

Question:

Using MVC 4 with EF code First. When doing linq to EF selects statements, the collections are being populated with what seems like the last record's data. And what's weirder, only some of the properties are repeated where others are not. It is best to show you by example:

Using this query returns the proper data:

var orders = db.Orders.ToList();

OrderID OrderTotal Name

1 215.00 Bob

2 415.00 Mark

3 315.50 Ralph

When I filter the Orders entity by a SubscriberID, which is a foreign key, like so:

var orders = db.Orders.Where(s => s.SubscriberId == 2).ToList();

The data end up looking like this:

1 315.50 Bob

2 315.50 Mark

3 315.50 Ralph

Notice how the OrderTotal is repeated but the names stay the same. Please note that this is not a view issue. When I look at the data in the collection while debugging in the controller, this is what I see. This does not appear to be the only place this is happening. I am seeing something similar with more complicated models - but I thought I'd start with the simplest sample. Thanks!

Answer1:

The problem was caused when I was setting default values on my models. Apparently using static was incorrect. So:

[ReadOnly(true)] [Display(Name = "Sub total")] private <strong>static</strong> decimal _OrderSubTotal = 0; public decimal OrderSubTotal { get { return _OrderSubTotal; } set { _OrderSubTotal = value; } }

Should be written as

[ReadOnly(true)] [Display(Name = "Sub total")] private decimal _OrderSubTotal = 0; public decimal OrderSubTotal { get { return _OrderSubTotal; } set { _OrderSubTotal = value; } }

Recommend

  • How to get the last running balance?
  • java array assignment confusion in shopping cart [closed]
  • Oracle - With a one to many relationship, select distinct rows based on a min value
  • The request was aborted: Could not create SSL/TLS secure channel.
  • How to compare two rows or get the fields which value is not match with the rows that compared?
  • Is it possible to display more than one item in a cell with a foreach condition?
  • Getting CDATA XML Section Using Linq
  • Entity Framework add local data to list from database
  • Casting a list of children to a list of parent types
  • jquery Ajax Request SyntaxError: Unexpected token <
  • Entity Framework Linq equals value or is null
  • How to find the first character in a given string, that appears only once [closed]
  • Entity Framework and the raw string query - SQL injection prevention
  • How to Convert IList to IList where SomeObject implements ISomeInterface using covariance in C# 4.0
  • Why doesn't this LINQ query work as expected?
  • Selecting and check if user exist in table without foreach in laravel blade
  • Get Distinct Parent Items using Lambda
  • Sort Data.Map by value and get all biggest values
  • Cannot create an instance of an interface (PagedList)
  • Linq distinct based on two columns
  • RavenDB OrderByDescending and Take - Incorrect Results
  • How do i convert a List to List in c#
  • Using Regex to split XML string before and after match
  • Linq to SQL left outer join using Lambda syntax and joining on 2 columns (composite join key)
  • Implement a writable serializer for multilevel nested relationships in django rest framework
  • SUM and GROUP BY in xquery with 1 xml file
  • ObjectMaterialize in EF not firing on first level query
  • Storing data from SQL in array
  • Why is it still possible to insert a foreign key that doesn't exist?
  • How to bind comma separated list of values to List
  • Stitching 2 images (OpenCV)
  • How to save dynamically created textboxes and their values
  • NHibernate manually control fetching
  • How do I remove all but some records based on a threshold?
  • How can I extract results of aggregate queries in slick?
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Volusion's generic SQL folder, functionality
  • bootstrap to use multiple ng-app
  • Turn off referential integrity in Derby? is it possible?