2012-02-18 35 views
0

我想建立一個簡單的一對多關係,其中一個列表可以有零個或多個任務,並且任務必須有一個關聯的列表。我想用單元測試來保證這種行爲,但我無法弄清楚這個關聯是如何發生的,這樣測試就會失敗(以後我會使用ExpectedExceptionAttribute,但我甚至不知道會拋出哪個異常然而)。如何確保EntityRef不能爲空?

任務即可成功提交,但我希望看到的LINQ抱怨缺少列表參考:

[TestMethod] 
public void SavingTaskWithoutListFails() 
{ 
    var task = new Task() { Title = "Test Task", Description = "This is the task description." }; 
    Db.Tasks.InsertOnSubmit(task); 
    Db.SubmitChanges();    
} 

我的類和關聯是這樣的:

[Table] 
public class Task 
{ 
    [Column(AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)] 
    public int TaskId { get; set; } 

    [Column(IsPrimaryKey = true)] 
    public string Title { get; set; } 

    [Column] 
    public string Description { get; set; } 

    [Column] 
    public int ListId { get; set; } 

    private EntityRef<List> _list; 

    [Association(Storage = "_list", ThisKey = "ListId")] 
    public List List 
    { 
     get { return _list.Entity; } 
     set { _list.Entity = value; } 
    } 
} 

與此:

[Table] 
public class List 
{ 
    [Column(AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)] 
    public int ListId { get; set; } 

    [Column(IsPrimaryKey = true)] 
    public string Title { get; set; } 

    private EntitySet<Task> _tasks; 

    [Association(Storage = "_tasks", OtherKey = "TaskId")] 
    public EntitySet<Task> Tasks 
    { 
     get { return _tasks; } 
     set 
     { 
      if (_tasks == null) 
      { 
       _tasks = new EntitySet<Task>(); 
      } 
      _tasks.Assign(value); 
     } 
    } 
} 

任何人都可以幫助正確的關聯或提示如何實現d行爲?

回答

0

您應該使用[Column(CanBeNull = false)]作爲關聯列來實現這一點。

編輯:

[Table] 
public class Task 
{ 
    [Column(IsPrimaryKey = true, AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)] 
    public int TaskId { get; set; } 

    [Column] 
    public string Title { get; set; } 

    [Column] 
    public string Description { get; set; } 

    [Column(CanBeNull = false)] 
    public int ListId { get; set; } 

    private EntityRef<List> _list; 

    [Association(Storage = "_list", ThisKey = "ListId", IsForeignKey = true, OtherKey = "ListId")] 
    public List List 
    { 
     get { return _list.Entity; } 
     set { _list.Entity = value; } 
    } 
} 

[Table] 
public class List 
{ 
    [Column(IsPrimaryKey = true, AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)] 
    public int ListId { get; set; } 

    [Column] 
    public string Title { get; set; } 

    private EntitySet<Task> _tasks; 

    [Association(Storage = "_tasks", OtherKey = "TaskId", ThisKey = "ListId")] 
    public EntitySet<Task> Tasks 
    { 
     get { return _tasks; } 
     set 
     { 
      if (_tasks == null) 
      { 
       _tasks = new EntitySet<Task>(); 
      } 
      _tasks.Assign(value); 
     } 
    } 
} 

我搬到IsPrimaryKeyId,而不是Title兩個表,設置關聯的一側爲IsForeignKey=true和填充都ThisKeyOtherKey爲協會的兩側。當我嘗試添加沒有ListId值的新任務時,SubmitChanges拋出SlqCEException並顯示以下消息:A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK_Task_List ]

+0

在[任務中將列[CanBeNull = false]設置爲ListId並不妨礙保存任務,但使保存列表失敗並帶有NullReference例外 – MazeT73 2012-02-18 18:01:45

+1

需要進行一些更改我編輯我的答案,使其更清晰,然後在這裏的評論 – MarcinJuraszek 2012-02-18 18:30:30

+0

謝謝!這有助於。我在列表方面還有其他一些問題,但我認爲我可以處理它們。 – MazeT73 2012-02-18 19:04:36

相關問題