Click here to Skip to main content
15,886,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 17-Aug-11 1:53am
v2
Comments
Herman<T>.Instance 17-Aug-11 8:58am    
what have you tried?
Legor 17-Aug-11 9:52am    
you are not clear

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.
 
Share this answer
 
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.
 
Share this answer
 
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:

C#
   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.
 
Share this answer
 

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