2010-06-11 152 views
1

我想在C#中以多對多的關係執行LINQ to SQL映射,但數據不是強制性的。
要清楚:
我有一個新聞網站/博客,並且有一張名爲Posts的表格。一個博客可以同時與多個類別相關聯,因此有一個名爲CategoriesPosts的表,該表與Posts表和Categories表的外鍵鏈接。如果在這種情況下很重要,我已經在每個表中使用了一個身份主鍵,每個表中都有一個id字段。
在C#中,我爲每個表定義了一個類,並儘可能明確地定義了每個字段。 Post類以及Category類有一個EntitySet鏈接到CategoryPost對象,並且CategoryPost類有2 EntityRef成員鏈接到每個其他類型的2個對象。LINQ的多對多映射

問題在於帖子可能與任何類別有關,也可能與某個類別有關,也可能是類別可能有帖子。我沒有找到一種方法來製作EntitySet<CategoryPost?>或類似的東西。

因此,當我添加第一篇文章,一切都很順利,沒有一個單一的SQL語句。另外,這篇文章出現在輸出中。當我嘗試添加第二個帖子時,出現異常,對象引用未設置爲對象的實例,與CategoryPost成員有關。

帖子:

[Table(Name="tm_posts")] 
public class Post : IDataErrorInfo 
{ 
    public Post() 
    { 
     //Initialization of NOT NULL fields with their default values 
    } 

    [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)] 
    public int ID { get; set; } 

    private EntitySet<CategoryPost> _categoryRef = new EntitySet<CategoryPost>(); 
    [Association(Name = "tm_rel_categories_posts_fk2", IsForeignKey = true, Storage = "_categoryRef", ThisKey = "ID", OtherKey = "PostID")] 
    public EntitySet<CategoryPost> CategoryRef 
    { 
     get { return _categoryRef; } 
     set { _categoryRef.Assign(value); } 
    } 
} 

CategoryPost

[Table(Name = "tm_rel_categories_posts")] 
public class CategoryPost 
{ 
    [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)] 
    public int ID { get; set; } 

    [Column(Name = "fk_post", DbType = "int", CanBeNull = false)] 
    public int PostID { get; set; } 

    [Column(Name = "fk_category", DbType = "int", CanBeNull = false)] 
    public int CategoryID { get; set; } 

    private EntityRef<Post> _post = new EntityRef<Post>(); 
    [Association(Name = "tm_rel_categories_posts_fk2", IsForeignKey = true, Storage = "_post", ThisKey = "PostID", OtherKey = "ID")] 
    public Post Post 
    { 
     get { return _post.Entity; } 
     set { _post.Entity = value; } 
    } 

    private EntityRef<Category> _category = new EntityRef<Category>(); 
    [Association(Name = "tm_rel_categories_posts_fk", IsForeignKey = true, Storage = "_category", ThisKey = "CategoryID", OtherKey = "ID")] 
    public Category Category 
    { 
     get { return _category.Entity; } 
     set { _category.Entity = value; } 
    } 
} 

類別

[Table(Name="tm_categories")] 
public class Category 
{ 
    [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)] 
    public int ID { get; set; } 

    [Column(Name = "fk_parent", DbType = "int", CanBeNull = true)] 
    public int ParentID { get; set; } 

    private EntityRef<Category> _parent = new EntityRef<Category>(); 
    [Association(Name = "tm_posts_fk2", IsForeignKey = true, Storage = "_parent", ThisKey = "ParentID", OtherKey = "ID")] 
    public Category Parent 
    { 
     get { return _parent.Entity; } 
     set { _parent.Entity = value; } 
    } 

    [Column(Name = "name", DbType = "varchar(100)", CanBeNull = false)] 
    public string Name { get; set; } 
} 

那我做錯了嗎?如何使插入不屬於任何類別的帖子成爲可能?如何插入沒有帖子的類別?

+0

你有沒有理由不使用設計師?它將爲你處理大部分... – Nate 2010-06-11 21:23:10

+0

主要是因爲我現在基於2本書:Apress「ASP.NET MVC Professional」和Manning「LINQ in action」,並且他們都強調硬編碼定義。 此外,我在這些實體類中有更多的功能,我不知道它在使用設計器時是否會正常工作。 (像構造函數與默認值初始值設定項,驗證程序,註釋)。 但是,謝謝。現在或週日將嘗試此。同時,我仍然接受關於如何糾正現有代碼的建議。 – AlexanderMP 2010-06-11 22:06:53

回答

0

看來這個錯誤與映射無關。映射是正確的。
正如我寫的,第一篇文章插入沒有問題,其餘未能插入。從數據庫中刪除後,我仍然無法添加帖子。很明顯,它與我在數據庫中是否有某些東西無關,只是因爲我對代碼做了一些修改。

那麼有什麼變化?在Apress的「ASP.NET MVC Pro」中,第一個例子說明了一種以迭代的方式驗證數據的方法(非聲明式,使用IDataErrorInfo提供的工具),我堅持使用它。我通過這個例子完成了所有的事情,並且應該驗證輸入的函數調用搞砸了我的數據流,並在提交到數據庫時拋出異常。

刪除了驗證,並且一切正常。

對不起,誤報。