Parent and Child object in SimpleRepository

混江龙づ霸主 提交于 2019-12-14 03:43:52

问题


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!


回答1:


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 :).




回答2:


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.



来源:https://stackoverflow.com/questions/1378292/parent-and-child-object-in-simplerepository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!