請記住,我沒有使用任何ORM工具。我剛剛寫了純ado.net代碼的類和方法看起來像下面。而且我也維護ID。C#多對多類建模問題
假設我有三個表Teacher
,Course
和CourseTeacher
(連接表)的多對多關係,我設計了我的類相應。
現在,假設最初所有的表都是空的。
我正在執行以下代碼第一次。
Course cpp = new Course();
cpp.CourseName = "CPP";
Course java = new Course();
java.CourseName = "Java";
Course cs = new Course();
cs.CourseName = "CS";
Teacher t1 = new Teacher();
t1.TeacherName = "ttt";
t1.AddCourse(cpp);
t1.AddCourse(java);
t1.AddCourse(cs);
t1.SaveOrUpdate();
Teacher t2 = new Teacher();
t2.TeacherName = "ppp";
t2.AddCourse(cpp);
t2.AddCourse(java);
t2.AddCourse(cs);
t2.SaveOrUpdate();
Teacher t3 = new Teacher();
t3.TeacherName = "mmm";
t3.AddCourse(cpp);
t3.AddCourse(java);
t3.AddCourse(cs);
t3.SaveOrUpdate();
我該如何處理我的代碼中的這種情況?
我應該拋出一個異常來告訴Course
- 表沒有任何數據嗎?
或者,同時填充Course
,Teacher
和CourseTeacher
表保持數據一致性?
第二個選項實際上是可能的(因爲Course
表沒有任何數據,程序試圖在CourseTable
中輸入數據會拋出數據庫異常)?
而你正在使用NHibernate? – chakrit 2009-10-24 03:16:06
不,先生。我沒有使用任何ORM工具。 – anonymous 2009-10-24 03:17:51
您在C#程序中使用標識符名稱java。那裏值得點,因爲有太多沒有說服力。 – 2009-10-24 03:21:12