2009-05-11 9 views
2

私は、城ActiveRecordで節約するために多対多の関係を得るために何時間も努力してきました。私は間違って何をしていますか?私はドキュメントやGoogleで何かを見つけることができません。データベースにはデータがあります。エラー:Castle Active Recordに多対多の関係を保存するにはどうすればいいですか?

コースには書籍との関係が多岐にわたります。

テストコード。

Database.Course c = new Database.Course(); 
c.Number = "CS 433"; 
c.Name = "Databases"; 
c.Size = 34; 
c.Books = Database.Book.FindAll(); 
c.Save(); 

はまた

foreach(Database.Book b in Database.Book.FindAll()){ 
    c.Books.Add(b); 
} 

データベースクラス

[ActiveRecord] 
public class Course : ActiveRecordValidationBase<Course> 
{ 
    private int? id; 
    private string number; 
    private string name; 
    private string description; 
    private int size; //number of students in class 

    //references 
    private IList books = new ArrayList(); 


    public override string ToString() 
    { 
     return FormattedName; 
    } 

    public string FormattedName 
    { 
     get 
     { 
      return string.Format("{0} - {1}", Number, Name); 
     } 
    } 

    [PrimaryKey] 
    public int? Id 
    { 
     get { return id; } 
     set { id = value; } 
    } 

    [Property, ValidateNonEmpty] 
    public string Number 
    { 
     get { return number; } 
     set { number = value; } 
    } 

    [Property, ValidateNonEmpty] 
    public string Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 

    [Property(ColumnType="StringClob")] 
    public string Description 
    { 
     get { return description; } 
     set { description = value; } 
    } 

    [Property] 
    public int Size 
    { 
     get { return size; } 
     set { size = value; } 
    } 

    [HasAndBelongsToMany(typeof(Book), 
    Table = "BookCourse", ColumnKey = "course_id", ColumnRef = "book_id", Inverse = true)] 
    public IList Books 
    { 
     get { return books; } 
     set { books = value; } 
    } 
} 

[ActiveRecord] 
public class Book : ActiveRecordValidationBase<Book> 
{ 
    private int? id; 
    private string title; 
    private string edition; 
    private string isbn; 
    private bool is_available_for_order; 
    //relations 
    private IList authors = new ArrayList(); 
    private IList bookordercount = new ArrayList(); 
    private IList courses = new ArrayList(); 
    private Inventory inventory; 

    public override string ToString() 
    { 
     return FormattedName; 
    } 

    public string FormattedName 
    { 
     //* 
     get { 
      string str; 
      if (Edition == null || Edition == "") 
       str = Title; 
      else 
       str = string.Format("{0} ({1})", Title, Edition); 

      if (Authors.Count != 0) 
      { 
       return string.Format("{0} by {1}", str, FormattedAuthors); 
      } 
      else 
      { 
       return str; 
      } 
     } 
     /*/ 
     get 
     { 
      return Title; 
     } 
     //*/ 
    } 

    public string FormattedAuthors 
    { 
     get 
     { 
      if (Authors.Count == 0) return ""; 

      StringBuilder sb = new StringBuilder(); 
      int i = 0, end = Authors.Count; 
      foreach (Author a in Authors) 
      { 
       i++; 
       sb.Append(a.FormattedName); 
       if (i != end) sb.Append("; "); 
      } 
      return sb.ToString(); 
     } 
    } 


    [PrimaryKey] 
    public int? Id 
    { 
     get { return id; } 
     set { id = value; } 
    } 

    [Property, ValidateNonEmpty] 
    public string Title 
    { 
     get { return title; } 
     set { title = value; } 
    } 

    [Property] 
    public string Edition 
    { 
     get { return edition; } 
     set { edition = value; } 
    } 

    [Property, ValidateNonEmpty] 
    public string Isbn 
    { 
     get { return isbn; } 
     set { isbn = value; } 
    } 

    [Property] 
    public bool IsAvailableForOrder 
    { 
     get { return is_available_for_order; } 
     set { is_available_for_order = value; } 
    } 

    //relations 

    [HasAndBelongsToMany(typeof(Author), 
    Table = "BookAuthor", ColumnKey = "book_id", ColumnRef = "author_id")] 
    public IList Authors 
    { 
     get { return authors; } 
     set { authors = value; } 
    } 

    [HasMany(typeof(BookOrderCount), Table = "BookOrderCounts", ColumnKey = "BookId")] 
    public IList BookOrderCount 
    { 
     get { return bookordercount; } 
     set { bookordercount = value; } 
    } 

    [HasAndBelongsToMany(typeof(Course), 
    Table = "BookCourse", ColumnKey = "book_id", ColumnRef = "course_id")] 
    public IList Courses 
    { 
     get { return courses; } 
     set { courses = value; } 
    } 

    [OneToOne] 
    public Inventory Inventory 
    { 
     get { return inventory; } 
     set { inventory = value; } 
    } 
} 
+0

を、オフに発射された最初の単一のエンティティを保存しようとしています"UNIQUE KEY制約の違反"私はすべてが正しく設定されていると思っています...考えています:( – rball

答えて

3

を動作しません。あなたがそれを配置したい場所を確認してくださいあなたは真=インバースを入れてください。 Castle AR docsから

の関係の1つをオーナーとして選択することは賢明です。反対側は、 書き込み不可、使用する必要があります 逆=真です。

関係の反対側にある真逆=を入れて、次のように:

[HasAndBelongsToMany(typeof(Book), 
    Table = "BookCourse", ColumnKey = "course_id", ColumnRef = "book_id")] 
    public IList<Book> Books 

[HasAndBelongsToMany(typeof(Course), 
    Table = "BookCourse", ColumnKey = "book_id", ColumnRef = "course_id", Inverse = true)] 
    public IList<Course> Courses 

また、両方のクラスの先頭に属性を追加する必要があります - 現時点では、彼らは知りませんどのテーブルにマップされているか。現在、あなたは、この持っている:この(「コースは」あなたのコースのテーブルの名前です)を追加します

public class Course : ActiveRecordBase<Course> 

を:同じ問題を抱えて

[ActiveRecord("course")] 
public class Course : ActiveRecordBase<Course> 
+0

あなたは何を意味しているのかわかりませんコースは 'inverse = true'を持っていますし、書籍はありません。 – epochwolf

+0

上記の私の編集に注意してください。キャッスルARのドキュメントは素晴らしいリソースです、 –

+1

@Gabriel:私はIListを使用したい IListの代わりに –

関連する問題