5 Replies - 557 Views - Last Post: 22 August 2016 - 04:07 PM

#1 ahmedba  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 24-January 14

add data to multiple table in same time using linq to entity

Posted 21 August 2016 - 09:11 AM

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 custom model as following

public class Customemployee
{
public string Name { get; set; }
public string Salary { get; set; }

public string Email { get; set; }
public int DistrictId { get; set; }

public List Courses { get; set; }
public List Langs { get; set; }
}
public class Empcourse
{
public int Id { get; set; }
public int EmployeeId { get; set; }
public int CourseId { get; set; }
}
public class Emplangauge
{
public int Id { get; set; }

public int LevelId { get; set; }
public int LanguageId { get; set; }

}

}


my controller empcourse is

public class empcourseController : Controller
{
mycourseEntities db = new mycourseEntities();
// GET: empcourse
public ActionResult Index()
{
return View();
}
public ActionResult Create()
{
ViewBag.CountryId = new SelectList(db.Countries.ToList(), "Id", "CountryName");
ViewBag.LanaguageId = new SelectList(db.Languages.ToList(), "Id", "LnaguageName");
ViewBag.LevelId = new SelectList(db.Levels.ToList(), "Id", "LevelName");
ViewBag.CourseId = new SelectList(db.Courses.ToList(), "Id", "CourseName");
return View();
}
[HttpPost]
public ActionResult Create(Customemployee cemp)
{
[size="6"][b]what i write in create method for insert to multiple table[/b][/size]
return View();
}
public JsonResult getcitybyid(int id)
{
db.Configuration.ProxyCreationEnabled = false;
return Json(db.Cities.Where(a => a.CountryId == id), JsonRequestbehavior.AllowGet);
}
public JsonResult getdistrictbyid(int id)
{
db.Configuration.ProxyCreationEnabled = false;
return Json(db.Destricts.Where(a => a.CityId == id), JsonRequestbehavior.AllowGet);
}
}
}

Posted Image
all model design and interface attached with message in image

This post has been edited by ahmedba: 21 August 2016 - 09:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: add data to multiple table in same time using linq to entity

#2 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2329
  • View blog
  • Posts: 9,356
  • Joined: 03-December 12

Re: add data to multiple table in same time using linq to entity

Posted 21 August 2016 - 02:58 PM

you would use separate insert queries, in a transaction.
Was This Post Helpful? 0
  • +
  • -

#3 ahmedba  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 24-January 14

Re: add data to multiple table in same time using linq to entity

Posted 22 August 2016 - 04:58 AM

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

This post has been edited by astonecipher: 22 August 2016 - 05:49 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2329
  • View blog
  • Posts: 9,356
  • Joined: 03-December 12

Re: add data to multiple table in same time using linq to entity

Posted 22 August 2016 - 05:50 AM

What is the declared type of this property, emp.EmployeeCourses?
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2329
  • View blog
  • Posts: 9,356
  • Joined: 03-December 12

Re: add data to multiple table in same time using linq to entity

Posted 22 August 2016 - 06:24 AM

found this, Linq insert
Was This Post Helpful? 0
  • +
  • -

#6 ahmedba  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 24-January 14

Re: add data to multiple table in same time using linq to entity

Posted 22 August 2016 - 04:07 PM

declared type is object
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1