2011-05-30 61 views
0

我有一個表「文章」如何使用LINQ映射到SQL一對多

private int id; 
    [ColumnAttribute(Storage = "id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)] 
    public int Id 
    { 
     get { return id; } 
     set { id = value; } 
    } 

    private string title; 
    [ColumnAttribute(Storage = "title", DbType = "NVarChar(250) NOT NULL", CanBeNull = false)] 
    public string Title 
    { 
     get { return title; } 
     set { title = value; } 
    } 

    private string description; 
    [ColumnAttribute(Storage = "description", DbType = "NVarChar(350) NOT NULL", CanBeNull = false)] 
    public string Description 
    { 
     get { return description; } 
     set { description = value; } 
    } 

和一張桌子評論

[Table(Name = "dbo.Comments")] 
public class CommentDto 
{ 
    private int id; 
    [ColumnAttribute(Storage = "id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)] 
    public int Id 
    { 
     get { return id; } 
     set { id = value; } 
    } 

    private string content; 
    [ColumnAttribute(Storage = "content", DbType = "NVarChar(600) NOT NULL", CanBeNull = false)] 
    public string Content 
    { 
     get { return content; } 
     set { content = value; } 
    } 

    private string date; 
    [ColumnAttribute(Storage = "date", DbType = "DateTime NOT NULL", CanBeNull = false)] 
    public string Date 
    { 
     get { return date; } 
     set { date = value; } 
    } 
} 

一個文章可以有許多意見和每個註釋可以放置由用戶

[TableAttribute(Name = "dbo.Users")] 
public class UserDto 
{ 
    private int id; 
    [ColumnAttribute(Storage = "id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)] 
    public int Id 
    { 
     get { return id; } 
     set { id = value; } 
    } 

    private string username; 
    [ColumnAttribute(Storage = "username", DbType = "NVarChar(150) NOT NULL", CanBeNull = false)] 
    public string Username 
    { 
     get { return username; } 
     set { username = value; } 
    } 

如何映射這些表之間的relatinship?

謝謝

+1

不應該先將映射關係映射到對象模型中嗎?我看不到你在你詳細描述的課程中描述的關係。 – BrokenGlass 2011-05-30 18:28:43

回答

0

我想你是手動創建你的表類。沒有必要這樣做。

將LINQ to SQL(dbml)文件添加到您的解決方案中,打開Server Explorer窗口並連接到您的數據庫, 將表拖放到您的dbml類的設計中。

如果表中存在外鍵約束,則鏈接將在兩個類中創建相應的屬性。

,如果你想這樣做手工(我不明白爲什麼), 建立與引用類的類型的屬性,這是需要的屬性:

[Association(Name="your_fk_constraint_name", Storage="name_of_your_private_backup_field", ThisKey="name_of_the_key_in_this_table", IsForeignKey=true)] 

希望我幫助一點點

+0

我想手動完成,因爲我可以在需要時通過創建和刪除數據庫來輕鬆地單元測試數據訪問層 – GigaPr 2011-05-30 18:50:20

+1

我沒有得到單元測試與手動創建表類所做的事情。 – 2011-05-30 19:20:36

0

你可以閱讀關於映射關聯here

你的情況:

class Article 
{ 
    private EntitySet<CommentDto> _Comments; 

    [Association(OtherKey = "ArticleID")] 
    public virtual IList<CommentDto> Comments 
    { 
      get 
      { 
       if (_Comments == null) 
        _Comments = new EntitySet<CommentDto>(); 
       return _Comments; 
      } 
      set 
      { 
       Comments.Assign(value); 
      } 
    } 
} 

class Comment 
{ 
    [Association(ThisKey="ArticleID")] 
    public ArticleDto Article { get; set; } 
} 

當然,你應該先使用條款ArticleID列添加到評論表中的數據庫。

下面的部分沒有出現在上面鏈接的MSDN代碼中,但沒有它,我在WCF服務中遇到了很多DTO問題。所以,現在我更願意將它添加到每個關聯中:

if (_Comments == null) 
    _Comments = new EntitySet<CommentDto>();