我試圖創建第一個使用EF代碼在我的MVC項目多到多的關係。我有一個場景,我有一個服務可以有許多類別可能有很多子類別。這一點似乎工作正常。ASP.NET MVC3 EF代碼第一次 - 許多一對多的關係
每個服務類別和子類別也可以有許多與其相關的HelpDeskMembers。這是我在創建關聯時遇到的問題。通過下面的課程,我期待EF創建Service_HelpDeskMembers,Category_HelpDeskMembers和Subcategory_HelpDeskMembers表。請有人引導我在正確的方向嗎?
P.S.在導航屬性中,我沒有使用虛擬的,因爲我使用AJAX和JSON創建服務/類別/子類別之間的級聯下拉列表,並通過使用「虛擬」,我得到一個循環引用錯誤。
這裏是我的班。
public class Service
{
[Key]
public int ServiceID { get; set; }
public string Title { get; set; }
public DateTime CreatedDate { get; set; }
public HelpDeskMember CreatedBy { get; set; }
public DateTime? DeletedDate { get; set; }
public HelpDeskMember DeletedBy { get; set; }
public ICollection<Category> Categories { get; set; }
public ICollection<HelpDeskMember> LinesOfSupport { get; set; }
}
public class Category
{
[Key]
public int CategoryID { get; set; }
[ForeignKey("Service")]
public int ServiceID { get; set; }
public string Title { get; set; }
public DateTime CreatedDate { get; set; }
public HelpDeskMember CreatedBy { get; set; }
public DateTime? DeletedDate { get; set; }
public HelpDeskMember DeletedBy { get; set; }
public Service Service { get; set; }
public ICollection<Subcategory> Subcategories { get; set; }
public ICollection<HelpDeskMember> LinesOfSupport { get; set; }
}
public class Subcategory
{
[Key]
public int SubcategoryID { get; set; }
[ForeignKey("Category")]
public int CategoryID { get; set; }
public string Title { get; set; }
public DateTime CreatedDate { get; set; }
public HelpDeskMember CreatedBy { get; set; }
public DateTime? DeletedDate { get; set; }
public HelpDeskMember DeletedBy { get; set; }
public Category Category { get; set; }
public ICollection<HelpDeskMember> LinesOfSupport { get; set; }
}
public class HelpDeskMember
{
public int HelpDeskMemberID { get; set; }
public string Name { get; set; }
public bool Admin { get; set; }
public bool Available { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? LastLogin { get; set; }
public DateTime? DeletedDate { get; set; }
public DateTime? DeletedBy { get; set; }
public ICollection<Service> Services { get; set; }
public ICollection<Category> Categories { get; set; }
public ICollection<Subcategory> Subcategories { get; set; }
}
在此先感謝。
謝謝!數據庫已經創建了我想要的。不敢相信第一次工作! :-) –
EF CodeFirst真棒:)看看目前在測試中的新Migrations的東西,這也是非常酷的 –