views:

281

answers:

3

How would it work in Subsonic's SimpleReporitory if I wanted to be able to have a 1 to many relationship between objects?

Would I have to create a bridge object and then build my parent object at runtime, or is this support built in?

What I am looking for is the folowing:

Adam's Example Shop...

Public Class Shop

    Private m_id As Integer
    Private m_Name As String
    Private m_Employees As List(Of Employee)

    Public Property Id() As Integer
        Get
            Return m_id
        End Get
        Set(ByVal value As Integer)
            m_id = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property

    Public Property Employees() As List(Of Employee)
        Get
            Return m_Employees
        End Get
        Set(ByVal value As List(Of Employee))
            m_Employees = value
        End Set
    End Property

End Class

Public Class Employee

    Private m_id As Integer
    Private m_Name As String

    Public Property Id() As Integer
        Get
            Return m_id
        End Get
        Set(ByVal value As Integer)
            m_id = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property

End Class
Main bits:
        Dim repo As New SimpleRepository("SubSonicObjectTest", SimpleRepositoryOptions.RunMigrations)

        Dim emplyee1 As New Employee
        emplyee1.Name = "Martin"
        Dim emplyee2 As New Employee
        emplyee2.Name = "Adam"

        Dim shop As New Shop
        shop.Name = "Sub Sonic Store"

        shop.Employees = New List(Of Employee)
        shop.Employees.Add(emplyee1)
        shop.Employees.Add(emplyee2)

        repo.Add(Of Shop)(shop)

I think this should create 3 tables:

Shops
Employees
ShopsToEmployees (or some other naming convention)

But I only get a Channels table!

+3  A: 

To create a one to many relationship you just have to create the object model, SubSonic should do the rest for you e.g.

public class Shop
{
  public int Id { get; set; }
  public String Name { get; set; }
  public List<Employee> Employees { get; set; }
}

public class Employee
{
  public int Id { get; set; }
  public String Name { get; set; }
}

EDIT: This should generate two tables when you run a migration not 3. The 3 three tables you describe in your question would represent a many to many relationship. Also in your example you're not saving your Employees, SubSonic does not cascade saves so you'll need to save your Shop then add the Employees to it and BatchSave the Employees.

Adam
I believe that your example if of a one-to-one relationship. I have updated my question with a sample object model for one-to-many...
MrHinsh
If I have as above I neither get a Content table nor any data saved..
MrHinsh
Sorry typo in my example, fixed now.
Adam
Also I think you'll need more than just a single Id in your Content class, try adding another property to it
Adam
Sory Adam, on testing I figured that out :).. I still do not get a Content table :(
MrHinsh
I updated my code in the question to better reflect your example...
MrHinsh
You're not saving your employees, this probably means SubSonic never runs the migration for that table, I've added some more info to my answer too.
Adam
Thats the answer I was looking for Adam, thanks... I was hoping that it would cascade... but it is not a deal breaker :)
MrHinsh
+3  A: 

I'm updating the SimpleRepo stuff currently to automatically create joined tables based on collections. Not easy to determine many/many vs 1/many - but I have some ideas :).

Rob Conery
Fantastic Rob :) I'll wait for the new version
MrHinsh
I'd love to see this too. Fluent NHibernate is the only option I'm aware of right now that does this, (see http://stackoverflow.com/questions/1610362/subsonic-can-anyone-provide-an-example-of-using-subsonic-to-persist-a-list-arra/1634084#1634084) but it looks complicated. The Subsonic approach is bound to be much simpler, based on my (admittedly limited) experience.
Tom Bushell
Rob, any update on when this will be in the package?
MrHinsh
I'm still the only one working on it - It's lonely out here. So no - no idea when.
Rob Conery
Rob, is this feature in a fork or branch somewhere I could try?
Keivan
A: 

Adam, To come back to your comment regarding the 2 classes you defined - I didn't really understand the cascade saving. Let's say there is a one-to-one relationship between Shop and Employee.

How would you save a Shop class? Like the below or am I missing something?

var shop = new Shop();
shop.name = "ikea";
var myEmployee = new Employee();
myEmployee.Name = "john doe";
shop.Employee.add(myEmployee);
repo.Add(shop);

public class Shop
{
  public int Id { get; set; }
  public String Name { get; set; }
  public Employee employee { get; set; }
}

public class Employee
{
  public int Id { get; set; }
  public String Name { get; set; }
}

thanks...I think the SimpleRepository is a great idea. Can't wait for Rob's updates

What I'd do in this situation is add a ShopId column to the Employee table that should reference the Id of the Shop class. Then specify the ShopId in the Employee class before saving it. If you think about it the Employee needs to know which shop they belong to so the ShopId is essential. Does that help?
Adam
Yes that helps... I still want cascade...
MrHinsh