Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
how can i represent my tables in database in c# classes
I have these tables

1) Employee Table with attributes(ID,Employee_Name,FK_Department)
2) Department Table with attributes (Department_Id,Department_Name)

it is one to many relationship which mean that one department have one or more Employees

i understood that the Employee class will have a reference to the Department_id
How can I make this.

thanks
Posted
Updated 26-Feb-12 11:31am
v2

Please see these articles:

http://en.wikipedia.org/wiki/Object-relational_mapping[^],
http://www.artima.com/intv/abstract3.html[^].

This is a very detailed but not very big article explaining the main ideas very clearly, with examples:
http://www.agiledata.org/essays/mappingObjects.html[^].

See also:
http://madgeek.com/Articles/ORMapping/EN/mapping.htm[^].

You could be interested to see the list of well-know ORM software and some comparison charts:
http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software[^],
http://en.wikipedia.org/wiki/Comparison_of_object-relational_mapping_software[^].

—SA
 
Share this answer
 
Comments
Аslam Iqbal 26-Feb-12 23:20pm    
good links. my 5\
Sergey Alexandrovich Kryukov 26-Feb-12 23:28pm    
Thank you, Aslam.
--SA
Wonde Tadesse 27-Feb-12 0:01am    
5+. For your reference. Even though it doesn't help for his question. Since OP didn't ask ORM Mapping. It just asks how he can associate the two classes in C#. Please read the question carefully. :)
Assuming that a department must contain more than one employee, which I believe it should be,then this can be One-to-many or Many-to-many. If the policy allow an employee to participate only one department, then it is One-to-many. If it allows more than one department it will be Many-to-many relationship. So you need to create an association class between employee and department say EmpDept.
So for your case, you don't have to create any association class.Here is what it looks like
C#
// This is One-to-Many relationship between Department and Employee classes

public class Employee(){

  public Department Department {get;set;};// Department reference from Department class
  public int Id {get;set;}
  public string Employee_Name {get;set;}
  public Employee() {
  }
}

public class Department {
  public List<Employee> Employees {get;set;} // List of Employees reference from Employee class.
  public Department_Id {get;set;}
  public string Department_Name {get;set;}
  public Department() {
  }

  // ...
  // The remaining private and public classes. Like AddEmployee, RemoveEmployee,FindDepartment ...
  // ...
}
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 26-Feb-12 20:44pm    
Well, maybe it is a nice introductory example, but it hardly gives an idea of the mapping in general case. I voted 4.

There is a good short article on the topic; I referenced it in my answer with some other material, please see.
--SA
Wonde Tadesse 27-Feb-12 0:00am    
Thanks. Well It completely answer's his question. Since OP didn't ask ORM mapping.No need to mention ORM in the answer. :)
VJ Reddy 23-Apr-12 19:30pm    
Good answer. 5!
Wonde Tadesse 24-Apr-12 19:32pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900