SimpleRepository中的Parent和Child对象

时间:2009-09-04 09:54:59

标签: subsonic subsonic3 simplerepository

如果我希望能够在对象之间建立1对多的关系,那么它如何在Subsonic的SimpleReporitory中运行?

我是否必须创建一个桥对象,然后在运行时构建我的父对象,或者内置这种支持?

我正在寻找的是:

Adam的示例商店......

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

主要位:

        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)

我认为这应该创建3个表:

商店
员工
ShopsToEmployees(或其他一些命名惯例)

但我只收到频道表!

2 个答案:

答案 0 :(得分:3)

要创建一对多关系,您只需要创建对象模型,SubSonic应该为您完成其余的工作。

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; }
}

编辑:当您运行迁移而不是3时,这应生成两个表。您在问题中描述的3个表将代表多对多关系。同样在您的示例中,您没有保存您的员工,SubSonic不会级联保存,因此您需要保存您的Shop然后将Employees添加到它并BatchSave Employees。

答案 1 :(得分:3)

我正在更新当前的SimpleRepo内容,以根据集合自动创建连接表。不容易确定很多/很多对1 /多 - 但我有一些想法:)。