I'm using Entity Framework 5, database first with SQL 2005.
When I try to load a parent entity with some of their children entities (using eager loading) I noticed that some children are being ignored.
This is the database structure I'm working with:
##tblCustomer
- CustomerId -> PK, nvarchar(15)
- FirstName
- LastName
- etc
##tblCustomerAddress
- AddressId -> PK, int
- CustomerId -> FK, nvarchar(15)
- AddressLine
- City
- State
- etc..
I'm trying to load a customer with all it's addresses, so I've been doing the following:
```
var customer = context.Customer.Include("CustomerAddresses").Where(x => x.CustomerId == "207233").FirstOrDefault();
```
According to my database, that customer has 3 addresses, but EF is only returning one record.
Now, if I do the following, I get all 3 addresses :
```
var addresses= context.CustomerAddresses.Where(x => x.CustomerId == "207233").ToArray();
```
Using SQL Profiler I was able to look at the SQL Query that EF5 generates to get the data from my database. I can say that the query is fine and returns all the rows I need, but somehow afterwards the data is getting lost.
I looked at the data in tblCustomerAddress and I noticed that the 2 records that are getting ignored have whitespaces in the FK CustomerId:
AddressId CustomerId
234 '207233 ' <- Is ignored by EF
344 '207233 ' <- Is ignored by EF
456 '207233' <- Loads correctly
So, first of all, you'll have to know that this database is populated by a lot of applications.. I actually don't have control over that, so a temporal workaround would be to trim the data, but it would brake again in the future.
So my conclusion is that there is some inconsistance in the way children objects are getting loaded. The proof is that when I try to load the child entity (CustomerAddresses) indirectly using eager loading, some data is lost, but when I load the child entity directly - all data is loaded correctly.
When I try to load a parent entity with some of their children entities (using eager loading) I noticed that some children are being ignored.
This is the database structure I'm working with:
##tblCustomer
- CustomerId -> PK, nvarchar(15)
- FirstName
- LastName
- etc
##tblCustomerAddress
- AddressId -> PK, int
- CustomerId -> FK, nvarchar(15)
- AddressLine
- City
- State
- etc..
I'm trying to load a customer with all it's addresses, so I've been doing the following:
```
var customer = context.Customer.Include("CustomerAddresses").Where(x => x.CustomerId == "207233").FirstOrDefault();
```
According to my database, that customer has 3 addresses, but EF is only returning one record.
Now, if I do the following, I get all 3 addresses :
```
var addresses= context.CustomerAddresses.Where(x => x.CustomerId == "207233").ToArray();
```
Using SQL Profiler I was able to look at the SQL Query that EF5 generates to get the data from my database. I can say that the query is fine and returns all the rows I need, but somehow afterwards the data is getting lost.
I looked at the data in tblCustomerAddress and I noticed that the 2 records that are getting ignored have whitespaces in the FK CustomerId:
AddressId CustomerId
234 '207233 ' <- Is ignored by EF
344 '207233 ' <- Is ignored by EF
456 '207233' <- Loads correctly
So, first of all, you'll have to know that this database is populated by a lot of applications.. I actually don't have control over that, so a temporal workaround would be to trim the data, but it would brake again in the future.
So my conclusion is that there is some inconsistance in the way children objects are getting loaded. The proof is that when I try to load the child entity (CustomerAddresses) indirectly using eager loading, some data is lost, but when I load the child entity directly - all data is loaded correctly.