Click here to Skip to main content
15,885,887 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to MVC

I have two tables
Employee and Users

Employee              
---------	      
PK_EmployeeID int      
EmpNo string	       	
EmpName string
...

Users
-------
UserName string
UserPassword string

UserName field in Users table is equal to EmpNo field in Employee.

I want to insert the record EmpNo into UserName simultaneously while creating new employee.
how can this be achieved in MVC3 ?
Do i need to make any Pk/Fk Relationship between these two tables.
Posted
Updated 2-Sep-13 1:26am
v2
Comments
Bala Selvanayagam 2-Sep-13 7:13am    
You could use a database trigger ?
saurabh kumar mahto 2-Sep-13 7:26am    
Thanks..
saurabh kumar mahto 3-Sep-13 1:36am    
Hi Bala..
Can you please help me out with some code sample regarding this..
Thanks a lot..
kesav prakash 2-Sep-13 8:00am    
bala can u post the query over here
Bala Selvanayagam 3-Sep-13 4:16am    
Please see my answer below

1 solution

Here the sample trigger further to my comments above

1. Create the trigger - run this in the SQL query analyser

CREATE TRIGGER tr_insert_user 
   ON  dbo.Employee
   AFTER INSERT
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    insert into users (UserName)
	select  EmpName from inserted

END
GO



2. Run a test query on the query analyser (insert data into the employee table)

SQL
INSERT INTO [dbo].[Employee]
           (
           [EmpName])
     VALUES
           ('Hello')


3.Now the trigger is automatically invoked (when ever you do a insert statement) and the users table is inserted with a new entry

Run the query on the database to check this out

select * from users
 
Share this answer
 
v2

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