2013-01-09 131 views
2

我對LINQ相當陌生,而且我遇到了一些問題。我嘗試了一段時間的谷歌搜索,但我仍然沒有找到任何精確的答案來解決我的問題,所以我也會問。LINQ問題:從數據庫讀取對象,出現異常

我已經在Microsoft SQL Server上建立了一個測試數據庫,其中有兩個表「Person2」和「Department2」。 Person2與Department2有多對一的關係。許多人只屬於一個部門。

PERSON2具有以下屬性:

  • ID(INT,主鍵)
  • 名稱(NCHAR(50))
  • phoneNumber的(NCHAR(10))
  • DepartmentID的(INT,外國主要用名: 「fk_Department2_DepartmentID」)

Department2具有以下屬性:

  • DepartmentID的(INT,主鍵)
  • DepartmentDesc(NCHAR(50))

的C#-code由四個班的兩個項目文件:人,部和方案(運行測試),另一個是TabellTest。 這是我試圖運行代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Data.Linq; 
using System.Data.Linq.Mapping; 

namespace DBTest 
{ 
    [Database] 
    public class TableTest : DataContext 
    { 
     public Table<Person> persons; 
     public Table<Department> departments; 

     public TabellTest(String ConnectionString): 
      base(ConnectionString){} 

    } 

} 


using System; 
using System.Collections; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Data.Linq; 
using System.Data.Linq.Mapping; 

namespace DBTest 
{ 
    [Table(Name = "Person2")] 
    public class Person 
    { 

     public Person() { } 

     [Column(IsPrimaryKey = true)] 
     private int id; 
     public int Id 
     { 
      get { return this.id; } 
      set { this.id = value; } 
     } 

     [Column] 
     private string name; 
     public string Name 
     { 
      get { return this.name; } 
      set { this.name = value; } 
     } 

     [Column] 
     private string phoneNumber; 
     public string PhoneNumber 
     { 
      get { return this.phoneNumber;} 
      set { this.phoneNumber = value;} 
     } 

     [Column (Name="DepartmentID")] 
     private int? personDepartmentID; 
     public int? PersonDepartmentID 
     { 
      get { return this.personDepartmentID; } 
      set { this.personDepartmentID = value; } 
     } 

     [Association(Name = "FK_Person_PersonDepartment", 
     IsForeignKey = true, Storage = "_department", ThisKey = "personDepartmentID")] 
     private EntityRef<Department> _department; 
     public Department Department 
     { 
      get { return _department.Entity; } 
      set { _department.Entity = value; } 
     } 



    } 

    [Table (Name = "Department2")] 
    public class Department { 

     public Department() { } 

     public Department(int departmentID, string departmentDesc) 
     { 
      this.departmentID = deparmentID; 
      this.departmentDesc = departmentDesc; 
     } 

     [Column(IsPrimaryKey = true)] 
     private int departmentID; 
     public int DepartmentID 
     { 
      get {return this.departmentID; } 
      set {this.departmentID = value; } 
     } 

     [Column] 
     private string departmentDesc; 
     public string DepartmentDesc 
     { 
      get { return this.departmentDesc; } 
      set { this.departmentDesc = value; } 
     } 

     private EntitySet<Person> _person = new EntitySet<Person>(); 
     [Association(Name = "FK_Person_PersonDepartment", 
     IsForeignKey = true, Storage = "_person", ThisKey = "departmentID", OtherKey="personDepartmentID")] 
     public EntitySet<Person> person 
     { 
      get { return _person; } 
      set { _person = value; } 
     } 


    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      TableTest Test = new TableTest("REMOVED FOR STACKOVERFLOW.COM, JUST ASSUME IT WORKS"); 

      foreach (Person pers in Test.persons) 
      { 
       Console.WriteLine(pers.Name + " " + pers.Id + " " + pers.PhoneNumber + " " + pers.Department.DepartmentID + " " + pers.Department.DepartmentDesc); 
      } 

      Console.WriteLine("\n\n"); 
      foreach (Department dep in Test.department) 
      { 
       Console.WriteLine(dep.DepartmentID + " " + dep.DepartmentDesc); 
       foreach (Person pers in dep.person) 
       { 
        Console.WriteLine(pers.Name + " " + pers.Id + " " + pers.PhoneNumber); 
       } 
      } 

     } 
    } 
} 

問題的核心是該代碼段中的類人:

[Association(Name = "FK_Person_PersonDepartment", IsForeignKey = true, 
    Storage = "_department", ThisKey = "personDepartmentID")] 
    private EntityRef<Department> _department; 
    public Department Department 
    { 
     get { return _department.Entity; } 
     set { _department.Entity = value; } 
    } 

每當我試着運行該程序,我得到此例外:

未處理的異常:System.InvalidOperationException:ThisKey列的數量與'Person'類型中關聯屬性'_department'的OtherKey列的數量不同 在.......等等等等等等

的溶液使用Google問題建議我的OtherKey屬性添加到關聯,在這種情況下,該協會代碼變成這樣,當我發現:

[Association(Name = "FK_Person_PersonDepartment", IsForeignKey = true, 
Storage = "_department", ThisKey = "personDepartmentID", OtherKey = "departmentID")] 

(我也試圖與資本d:OtherKey = 「DepartmentID的」)

當我這樣做,我得到這個異常:

未處理的異常:System.InvalidOperationException:找不到類型爲'EntityRef 1´. The key may be wrong or the field or property on ´EntityRef 1'的關鍵'departmentID'的關鍵成員'departmentID'已更改名稱。 在....等等等等

具有諷刺意味的是,該協會段自處,這與EntitySet的操作同時使用的按鍵(只接通ThisKey和OtherKey),和作品。 換句話說:我無法從Person類的數據庫中獲取Department對象,但它可以在Department-Code中獲取Person對象的集合。

現在,親愛的讀者和程序員,你建議我做什麼?

回答

1

看起來它可能只是訂單 - 您正在修改私人會員而非公衆;你試過這個嗎?

private EntityRef<Department> _department; 
[Association(Name = "FK_Person_PersonDepartment", IsForeignKey = true, 
    Storage = "_department", ThisKey = "personDepartmentID", OtherKey = "departmentID")] 
public Department Department 
{ 
    get { return _department.Entity; } 
    set { _department.Entity = value; } 
} 
+0

(作者劉海自己的辦公桌上頭) 衛生署!... 它現在。非常感謝 :) –

相關問題