n-m relation , How To ??

Oct 20, 2008 at 6:32 PM
Edited Oct 20, 2008 at 10:16 PM
Hello, And congratulation for your very interesting ORM.

i am playing a bit with this ORM , I can do 1-1 and 1-n relations but ,I am having trouble to figure out how to do  a n-m relation .
Could someone show me a concret example of a n-m relation , how do you set up the ForeignKey attribute ?


Thank you in advance
Coordinator
Oct 21, 2008 at 2:20 AM
Hi Alain, n-m relationships make use of an intermediary entity and Retina.NET needs a little extra configuration but its totally supported.
Here I'm including a sample relationship of Books & Authors, with the intermediary entity also:

[

TypeStorage(TableName = "Authors")]
public class Author : Entity
    [
FieldStorage(ColumnName = "Id",IsKey = true, IsIdentity = false)] 
    private string _Id; 
    
    
public string Id 
    { 
        
get {return _Id;} 
        set {_Id=value; } 
    }

 

[

FieldStorage]
public string LastName;

 

 

[

FieldStorage]
public string FirstName;

 

 

public Author() {}

 

 

public Author(string p_FirstName, string p_LastName) { 
    LastName = p_LastName; 
    FirstName = p_FirstName; 
}

 

}

[

TypeStorage(TableName = "BooksAuthors")]
public class BooksAuthors : Entity {

 

[

FieldStorage(IsKey = true)]
public readonly string BookId;

 

[

FieldStorage(IsKey = true)]
public readonly string AuthorId;

 

}

[

TypeStorage(TableName = "Books")]
public class Book : Entity {

 

[

FieldStorage(ColumnName = "Id",IsKey = true, IsIdentity = false)]
private string _Id;

 

 

public string Id

    
get{return _Id;} 
    
set{_Id=value;}
}

 

[

FieldStorage]
public string Title;

 

[

FieldStorage]
public DateTime PublishDate;

 

[

ChildStorage(ChildType=typeof(Author), LazyLoad=LazyLoadMode.Off, IsReference=false, AssociateType=typeof(BooksAuthors))]
[
AssociateParentForeignKey(ParentFieldName= "Id", ParentAssociatedFieldName = "BookId")]
[
AssociateChildForeignKey(ChildFieldName = "Id", ChildAssociatedFieldName = "AuthorId")]
public ChildCollection<Author> Authors;

 

 

 

public Book() {}

 

 

public Book(string p_Title, DateTime p_PublishDate)

    Title = p_Title; 
    PublishDate = p_PublishDate;
}

 

}

Oct 21, 2008 at 4:56 PM
Thank You very much !

Oct 21, 2008 at 7:17 PM
Hum...

At the present time I am trying to create an example with all the different relations : 0-1, 1-1, 1, n, n-m, etc.

With this example, we can not do such thing : myAuthor.Books... or myBook.Authors... ?

If BooksAuthors represents the link between the tables, it sould only contain ID, no ? I do not understand the BooksAuthors' members...

Thanks.
Coordinator
Oct 21, 2008 at 8:09 PM
I have taken this code from a working sample, so it works as intended.
The table (or Entity) BooksAuthors is the intermediary link between Books & Authors, and it has two foreign key properties (BookId & AuthorId) as expected. Every n-m relationship needs an intermediary table to store the relationship between the other two entities (each entity primary key related to each other).

Please give it a closer look and let me know if you have any other doubt.
Best regards,
Andrés.
Nov 16, 2009 at 12:11 PM
Edited Nov 16, 2009 at 1:21 PM

Hi andresv,

I understand the exemple above. I have a question.

If I shift the part of code from the Book class:

[ChildStorage(ChildType=typeof(Author), LazyLoad=LazyLoadMode.Off, IsReference=false, AssociateType=typeof(BooksAuthors))]
[
AssociateParentForeignKey(ParentFieldName= "Id", ParentAssociatedFieldName = "BookId")]
[
AssociateChildForeignKey(ChildFieldName = "Id", ChildAssociatedFieldName = "AuthorId")]
public ChildCollection<Author> Authors;

into the Author class by modifying as below:

[ChildStorage(ChildType=typeof(Book), LazyLoad=LazyLoadMode.Off, IsReference=false, AssociateType=typeof(BooksAuthors))]
[
AssociateParentForeignKey(ParentFieldName= "Id", ParentAssociatedFieldName = "AuthorId")]
[
AssociateChildForeignKey(ChildFieldName = "Id", ChildAssociatedFieldName = "BookId")]
public ChildCollection<Book> Books;

I have the same rezult. Isn't right ?

10x,

Daniel.