Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: LINQ C#4.0 MVC
I am new to MVC and LINQ. I am developing an Employee Management System.
 
When I add an Employee, I need to add his details in the other mapping tables also.
 
For ex: An employee has his basic details like EmpID, EmpName etc.
 
However the other Department table has other details related to his Department based on the EmpID from the Employee table.
 
How can I use the information from both the tables, I need to use them for Listing, Creating, Editing, Deleting etc.
 
Please help !!!
 
Thanks,
Ankur
 
If I am not clear, do let me know, but plz help
Posted 16-Aug-11 21:12pm
Edited 17-Aug-11 1:53am
v2
Comments
digimanus at 17-Aug-11 8:58am
   
what have you tried?
Legor at 17-Aug-11 9:52am
   
you are not clear
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If I understand you correctly you have a front end where you would like to add employees. From there you need to update more than one table. Your best option would be to create a stored procedure in your database to accept the parameters of your data. The stored procedure will need to then update each table as required.
 
If you are updateing then you would create an update procedure, again to amend all tables necessary.
 
I hope this helps.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

With LINQ the code side of things should pretty much work just like objects (I'm assuming by Tables you mean DB tables, I'm a little unclear about your question. You shouldn't think of Employee as something from a table as much as an obect that happens to be stored in a table.To set this up, you need to add a Department type. The Employee should have a Department[s] property (i.e. one, or more, related Department) and vice versa. You can then access the properties as normal objects and write to the backing store through LINQ.
 
If you are using the Entity Framework, if you add the Department Table as a new type in your entity model, it should add the properties for you with the correct cardinality, as long as you have normalised your Database properly. With LINQ to SQL you can look here[^] about how to define the relationship.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

In the event you are displaying the employee and the additional data, and want an update of all that data, using Entity Framework gives you an easy approach.
 
Create your own object that is comprised of your various components -- known as a composite object. So a way of doing this for example would be:
 
   public class CompositeEmployee:Employee
   {
        public ObservableCollection<students> StudentList{get;set;}
        public Department {get;set;}
        public HumanResourceInfo{get;set;}
   }
</students>
 
In your server side you take the composite in for UpdateEmployee(CompositeEmployee)
and it will break out the pieces and do the separate updates.
 
You do have one thing to struggle with and that is how to handle things of one of the updates fails. Rollback the update or keep running returning a list of exceptions/errors that occurred.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 377
1 Nirav Prabtani 284
2 Richard Deeming 190
3 _Amy 170
4 Mehdi Gholam 140
0 Nirav Prabtani 265
1 OriginalGriff 177
2 _Amy 170
3 Mehdi Gholam 105
4 Maciej Los 90


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 18 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100