SimpleRepository 中的父对象和子对象

发布于 2024-08-04 07:21:16 字数 1950 浏览 3 评论 0原文

如果我希望能够在对象之间建立一对多的关系,那么在 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(或其他一些命名约定)

但我只得到一个 Channels 表!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遇到 2024-08-11 07:21:16

我当前正在更新 SimpleRepo 内容,以根据集合自动创建连接表。确定多/多与 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 :).

-残月青衣踏尘吟 2024-08-11 07:21:16

要创建一对多关系,您只需创建对象模型,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 不会级联保存,因此您需要保存您的商店,然后将员工添加到其中并批量保存员工。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文