How to insert data to multiple entity in same time using linq to entity

Posted by Ahmedsa under LINQ on 8/21/2016 | Points: 10 | Views : 338 | Status : [Member] | Replies : 6
I need to make multiple insert to multiple table have relation with each other

all id in all table is identity and already do model relation to it

in visual studio 2015

what i need actually when user click submit

Save the following data

Name,Email,Salary,DistrictId in table Employee

EmployeeId,CourseId in table EmployeeCourse

EmployeeId,LanaguageId,LevelId in table EmployeeLangage


what i write in create function in empcourse controller

my diagram and interface found in link bellow
http://www.mediafire.com/view/mn44bl69zkrjukp/Interface3.jpg




Responses

Posted by: Sheonarayan on: 8/21/2016 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
If I understand your query, it is as simple as building instances of all three models and setting their property and calling .Add method one after another.

Look at this article as an example http://www.dotnetfunda.com/articles/show/1690/simple-way-of-using-transactions-in-adonet-entity-framework.

Keep calling db.SaveChanges(); after every .Add(model) method. When you insert Employee, the employee object will have the EmployeeId property set from the database automatically, use that property to set EmployeeId into remaining two models.

Hope you got this.

Thanks


Regards,
Sheo Narayan
http://www.dotnetfunda.com

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ahmedsa on: 8/21/2016 [Member] Starter | Points: 25

Up
0
Down
can you explain to me if possible by code
public ActionResult Create(Customemployee cemp)

{
//object from database
dbmycourseentity db=new dbmycourseentity();
db.Employee.add(cemp)
db.Savechanges();

return View();
}

HOW to get employee id
can you explain what you mean
When you insert Employee, the employee object will have the EmployeeId property set from the database automatically, use that property to set EmployeeId into remaining two models.
please help me

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ahmedsa on: 8/21/2016 [Member] Starter | Points: 25

Up
0
Down
can you explain more

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 8/21/2016 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Here you can get the employeeid using cemp.EmployeeId after db.SaveChanges() method call. Use this to set EmployeeId property of other objects.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ahmedsa on: 8/22/2016 [Member] Starter | Points: 25

Up
0
Down
i try with this :

Employee emp = new Employee();

emp.Name = form["Name"].ToString();
emp.Salary =Convert.ToInt32(form["Salary"]);
emp.Email = form["Email"].ToString();
emp.DistrictId = Convert.ToInt32(form["DistrictId"]);

var coursesId = form["Courses"];

string[] coursesIds = null;

if (coursesId != null)
{
coursesIds = coursesId.Split(',');
}

List<Empcourse> courses = new List<Empcourse>();

foreach (var item in coursesIds)
{
Empcourse ecour = new Empcourse();
ecour.EmployeeId = emp.Id;
ecour.CourseId = Convert.ToInt32(item);
courses.Add(ecour);
}
// emp.EmployeeCourses=courses
emp.EmployeeCourses=courses;
db.Employees.Add(emp);
db.SaveChanges();


but i face problem in this line

emp.EmployeeCourses=courses;


how to assign list to object

it give me this error

cannot implicity convert convert System.collection.Generic.list to System.collection.Generic.Icollection

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 8/22/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Obviously, you cannot do so. Change the either way instead.

--
Thanks & Regards,
RNA Team

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response