I am using c# MVC I am trying to create 2 new records in 2 tables – employee and address table.
I have the following so far:
db.employee.AddObject(empmodel.employee);
db.address.AddObject(empmodel.address);
db.SaveChanges();
When an employee record is created, it creates an EmpID record which is autogenerated. I need to get that EmpID and create a new record in the address table with that EmpID as as there is a primary foreign key relationship between the 2 tables.
I am not sure how to get the EmpID from employee table and then create a new record for the address table. I thought I could get it after AddObject but it did not create an employee record.
I know you’re not supposed to just give feedback, but in this case, this answer is right on, no need to go further. If you create a model, using a database table, as long as that database table has an ID field. It will create the
get/setmethods for you. In mine, it even created a more elaborateget/setmethod probably because the name of my ID field is ProdID. But you can expand themodel.csfile, (after you create the model from the database) and see theget/setmethod these guys are talking about. In the case you are using aGUIDyou can just useThe
NewGuid()method in the controller create function.