2013-07-18 12 views
0

我有一個遞歸數據結構,它有一個(空)父和一個(空)子集合。設置自引用類

我想將該數據結構保存到windows phone上的本地數據庫中。 我設置了一個DataContext,它工作的很好,但是當我想插入一個新的對象(它沒有父母和子女)時,我得到了「對象引用未設置爲對象的實例」。錯誤。

搜索後,我發現此線程LINQ to SQL: Self-referencing entity - ParentID/ChildID association,它聲明,由於父對象和子對象爲空,所以引發此異常。 但是,因爲這是一個允許的狀態,這不應該阻止我的代碼正常運行。

所以問題是:如何設置一個自引用類。

我迄今所做的:

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Data.Linq; 
using System.Data.Linq.Mapping; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Runtime.CompilerServices; 

namespace Notepad.Codes.Models 
{ 

    [Table] 
    public class DirectoryItem 
    { 
     [Column(IsVersion=true)] 
     private Binary version; 

     [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)] 
     public int primaryKey; 

     private DirectoryItem parent; 
     [Association(Storage = "parent", ThisKey = "primaryKey", OtherKey = "primaryKey")] 
     public DirectoryItem Parent 
     { 
      get 
      { 
       return parent; 
      } 
      set 
      { 
       this.SetProperty(ref this.parent, value); 
      } 
     } 

     private EntitySet<DirectoryItem > children; 
     [Association(Storage = "children", OtherKey = "primaryKey")] 
     public EntitySet<DirectoryItem > Children 
     { 
      get 
      { 
       if(children == null) 
        children = new EntitySet<DirectoryItem >(); 
       return children; 
      } 
      set 
      { 
       if (this.PropertyChanging != null) 
        PropertyChanging(this, new PropertyChangingEventArgs("Children")); 
       this.children.Assign(value); 
       if (this.PropertyChanged != null) 
        PropertyChanged(this, new PropertyChangedEventArgs("Children")); 
      } 
     } 
    } 
} 

可能有人請告訴我,我怎麼也得使用或更改Associoaton -Attribute,這樣我可以插入null的父母和孩子到我的數據庫?

+0

沒有必要在標籤標題,有一個標籤系統。請閱讀http://meta.stackexchange.com/q/19190/147072瞭解更多信息。 – Patrick

+0

你可以把你的例外的完整的調用堆棧嗎? –

回答

0

我解決了我與Northwind的問題。 我按照這些步驟(我一一列舉了,因爲MSDN是一個嚴重改變的地方:)

  1. http://www.microsoft.com/en-us/download/details.aspx?id=23654
  2. 下載並運行安裝程序
  3. 打開Visual Studio(< 2012)
  4. 開放服務器瀏覽器,打開快捷菜單數據連接並選擇添加連接
  5. 選擇微軟SQL服務器數據庫文件
  6. 選擇瀏覽並瀏覽到安裝的數據庫(對我來說它是根據安裝C:\Sample Database(或類似)
  7. 使用Windows身份驗證並單擊確定*
  8. 從northwind數據庫中選擇所有表格,並將它們拖放到對象關係設計器中
  9. 現在您可以切換到文件後面的代碼並查看所有邏輯和屬性

而對於我來說,解決現在的樣子:

[Table] 
public class DirectoryItem 
{ 
    [Column(IsVersion=true)] 
    private Binary version; 

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)] 
    public int primaryKey; 

    private System.Nullable<int> parentId; 
    [Column(Storage = "parentId", DbType="Int")] 
    public System.Nullable<int> ParentId 
    { 
     get 
     { 
      return this.parentId; 
     } 
     set 
     { 
      this.SetProperty(ref this.parentId, value); 
     } 
    } 

    private EntityRef<DirectoryItem > parent; 
    [Association(Name = "DirectoryItem_parent", Storage = "parent", ThisKey = "ParentId", OtherKey = "primaryKey", IsForeignKey = true)] 
    public DirectoryItem Parent 
    { 
     get 
     { 
      return parent.Entity; 
     } 
     set 
     { 
      if (this.PropertyChanging != null) 
       PropertyChanging(this, new PropertyChangingEventArgs("Parent")); 

      parent.Entity = value; 

      if (this.PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs("Parent")); 
     } 
    } 

    private EntitySet<DirectoryItem > children; 
    [Association(Name = "DirectoryItem_DirectoryItem", Storage = "Children", ThisKey = "primaryKey", OtherKey = "ParentId")] 
    public EntitySet<DirectoryItem > Children 
    { 
     get 
     { 
      if (children == null) 
       children = new EntitySet<DirectoryItem >(); 
      return children; 
     } 
     set 
     { 
      if (this.PropertyChanging != null) 
       PropertyChanging(this, new PropertyChangingEventArgs("Children")); 

      if (children == null) 
       children = new EntitySet<DirectoryItem >(); 
      this.children.Assign(value); 

      if (this.PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs("Children")); 
     } 
    } 
} 

- 附錄:

  1. instrucitons安裝示例數據庫:http://msdn.microsoft.com/en-us/library/vstudio/8b6y4c7s.aspx
1

有一個在Children setter方法存在問題:

if (this.PropertyChanging != null) 
    PropertyChanging(this, new PropertyChangingEventArgs("Children")); 

this.children.Assign(value); 

if (this.PropertyChanged != null) 
    PropertyChanged(this, new PropertyChangedEventArgs("Children")); 

你打電話this.children.Assign即使children可能未初始化。使用與您的吸氣劑相同的支票來防止此問題:

if(children == null) 
    children = new EntitySet<DirectoryItem >(); 

this.children.Assign(value); 
+0

謝謝。你把我放在正確的軌道上。孩子們提出了這個錯誤,但這不是分配,而是協會。它在我設置'Storage ='Children''時起作用。但我仍在調查這個問題... –