开发者

1 to many relationship

i am trying to create an objectmodel based on a databasemodel. The database model contains foreign key constraints. For example I have 3 tables : Company, Employee and ShareHolder. A Company has 1 or more Employees, and a Company has 1 or more ShareHolders. The Employee and ShareHolder table have a foreign key reference to the Company table. So when I create my object Model I get:

public class Company
{
    public string CompanyID {get;set;}
    List<Employee> Employees {get;set;}开发者_运维百科
    List<ShareHolder> ShareHolders {get;set;}
}

Is this the handiest way to do this?


It looks right to me, i would do it the same.

public class Company
{
    public string CompanyID {get;set;}
    List<Employee> Employees {get;set;}
    List<ShareHolder> ShareHolders {get;set;}
}

public class Employee
{
    public string CompanyID {get;set;}
    //...
}

public class ShareHolder
{
    public string CompanyID {get;set;}
    //..
}


Yes, that is the most obvious approach. Especially if your application is Company focused.

Depending on how you will use your objects in your application you might have back links from Employee and ShareHolder, this way you can easily navigate from Employee and ShareHolder back to Company without excessive searching.

public class Person
{
    public string Name {get;set;}
}

public class Employee : Person
{
    public Company Workplace {get;set;} // might be List<Company> depending on your application needs
}

public class ShareHolder : Person
{
    public List<Company> OwnedCompanies {get;set;}
}


Since its for a view model I'd name it CompanyDisplay or CompanyInput based on what the view is doing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜