SubSonic 3 - Simple repository - One to many relations

别来无恙 提交于 2019-12-08 04:55:00

问题


It's the first time I use Subsonic.

Let say I have those classes :

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

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

An employee is related to a store with is hired date. This means that in the database I will have a middle table With StoreId, EmployeeId, StartDate, EndDate

UPDATE

An employee can work to the StoreA from 2009-01-01 to 2009-04-04 and work for StoreB from 2009-04-05 to ... And I don't want that my data table repeat all the information of my employee each time an employee change the store he working for. In this example employee have only a name, but lets say an employee got 10 property (adress, age, gender ...)

How could I achieve that ?


回答1:


Based on your comment and the updated question it looks like you want something like the following:

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

public class StoreEmployee
{
    public int Id { get; set; }
    public Employee Employee { get; set; }
    public Store Store { get; set; }
    public DateTime HiredDate { get; set; }
}

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



回答2:


You'll actually need a many-to-many relationship which will join an Employee record to a Store Record, with a payload of Start and End Dates.

The Objects will look like this:

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

public class Employee
{
    public int Id { get; set; }
    public String Name { get; set; }
    public IList<EmploymentTerm> EmploymentTerms { get; set; }
}

public class EmploymentTerm
{
    public int Id { get; set; }
    public Store Store { get; set; }
    public Employee Employee { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
}

Did this freehand so there could be a couple errors.



来源:https://stackoverflow.com/questions/1300207/subsonic-3-simple-repository-one-to-many-relations

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