我想在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; }
}
那我做錯了嗎?如何使插入不屬於任何類別的帖子成爲可能?如何插入沒有帖子的類別?
你有沒有理由不使用設計師?它將爲你處理大部分... – Nate 2010-06-11 21:23:10
主要是因爲我現在基於2本書:Apress「ASP.NET MVC Professional」和Manning「LINQ in action」,並且他們都強調硬編碼定義。 此外,我在這些實體類中有更多的功能,我不知道它在使用設計器時是否會正常工作。 (像構造函數與默認值初始值設定項,驗證程序,註釋)。 但是,謝謝。現在或週日將嘗試此。同時,我仍然接受關於如何糾正現有代碼的建議。 – AlexanderMP 2010-06-11 22:06:53