Reputation: 8249
I'm trying to query an Oracle DB (version 11.2.0.2.0) in a project that uses ADO.NET, compiled at .NET 4 (I would compile at .NET 4.5 if Windows XP was compatible with it), and uses linq to entities to perform the query. I'm also using Oracle Developer Tools for Visual Studio and ODP.NET version 11.2.0.3.20. However, whenever the query is performed in the application, I get back a list of objects with incorrect data. What I mean by incorrect data is this: I have 5 properties in the entity object and two of those properties are not set as entity keys. The two properties that ARE NOT set as entity keys contain the same data for all records returned...but when I manually do a query on the DB with a DB query client, I get different values in those columns for each record!
Here's the auto generated code that I'm getting (using DBContext but I've verified that this happens with ObjectContext too):
public partial class EntityObj
{
public string EntityKey1 { get; set; }
public string EntityKey2 { get; set; }
public string EntityKey3 { get; set; }
public Nullable<decimal> NonEntityKey1 { get; set; }
public string NonEntityKey2 { get; set; }
}
Here's my linq to entities code:
using (Entities context = new Entities())
{
string formattedStr = Decimal.Parse(str).ToString();
var objs = (from obj in context.EntityObjs
where obj.Number == formattedStr
orderby obj.EntityKey3 , obj.NonEntityKey2, obj.NonEntityKey1
select obj);
// Process results...
}
Here is an example of the result set that I get when I manually do the query:
| EntityKey1 | EntityKey2 | EntityKey3 | NonEntityKey1 | NonEntityKey2 | ---------------------------------------------------------------------------------- | Val1.1 | Val1.2 | Val1.3 | 1 | Val1.4 | ---------------------------------------------------------------------------------- | Val2.1 | Val2.2 | Val2.3 | 2 | Val2.4 | ---------------------------------------------------------------------------------- | Val3.1 | Val3.2 | Val3.3 | 3 | Val3.4 | ---------------------------------------------------------------------------------- | Val4.1 | Val4.2 | Val4.3 | 4 | Val4.4 |
Here is an example of the result set that I get when I use the linq to entities:
| EntityKey1 | EntityKey2 | EntityKey3 | NonEntityKey1 | NonEntityKey2 | ---------------------------------------------------------------------------------- | Val1.1 | Val1.2 | Val1.3 | 1 | Val1.4 | ---------------------------------------------------------------------------------- | Val2.1 | Val2.2 | Val2.3 | 1 | Val1.4 | ---------------------------------------------------------------------------------- | Val3.1 | Val3.2 | Val3.3 | 1 | Val1.4 | ---------------------------------------------------------------------------------- | Val4.1 | Val4.2 | Val4.3 | 1 | Val1.4 |
Why am I seeing the results that I'm seeing and how do I fix it to return the correct data without making the non entity key fields entity keys? (They can't be keys cause they are nullable)
EDIT:
I should also point out that when I do the following query I don't see the same issues...but I really would like things to work as I've stated before.
using (Entities context = new Entities())
{
string formattedStr = Decimal.Parse(str).ToString();
var objs = (from obj in context.EntityObjs
where obj.Number == formattedStr
orderby obj.EntityKey3 , obj.NonEntityKey2, obj.NonEntityKey1
select new
{
EntityKey1 = obj.EntityKey1,
EntityKey2 = obj.EntityKey2,
EntityKey3 = obj.EntityKey3,
NonEntityKey1 = obj.NonEntityKey1,
NonEntityKey2 = obj.NonEntityKey2
});
// Process results...
}
Upvotes: 0
Views: 466