[英]Oracle linq to entities query returning duplicated columns
我正在嘗試在使用ADO.NET的項目中查詢Oracle DB(版本11.2.0.2.0),該項目在.NET 4上編譯(如果Windows XP與之兼容,我將在.NET 4.5上編譯),並使用linq到實體執行查詢。 我還使用了適用於Visual Studio和ODP.NET版本11.2.0.3.20的Oracle開發工具。 但是,每當在應用程序中執行查詢時,我都會獲取帶有錯誤數據的對象列表。 我用不正確的數據表示的意思是:我在實體對象中有5個屬性,而其中兩個屬性未設置為實體鍵。 未設置為實體鍵的兩個屬性對於返回的所有記錄都包含相同的數據...但是,當我使用DB查詢客戶端在DB上手動執行查詢時,我在每個記錄的那些列中獲得了不同的值!
這是我得到的自動生成的代碼(使用DBContext,但我已經驗證了ObjectContext也會發生這種情況):
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; }
}
這是我的實體代碼的linq:
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...
}
這是當我手動執行查詢時得到的結果集的示例:
| 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 |
這是將linq用於實體時得到的結果集的示例:
| 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 |
為什么我會看到自己看到的結果,以及如何在不使非實體鍵字段成為實體鍵的情況下修復它以返回正確的數據? (它們不能是鍵,因為它們可以為空)
編輯:
我還應該指出,當我執行以下查詢時,我沒有看到相同的問題...但是我確實希望事情能夠像我之前所說的那樣工作。
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...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.