I just have a simple question about bridge(link) table. I have two tables that has a many-to-many relation and Visual Studio auto generates the bridge table with the primary key from those two tables. I'm a little bit confused about how to read and write data from and to the tables.
When I read data, do I query and fetch data from just one or both of the two tables that has a many-to-many relation? But how do I handle the bridge table?
And when I write data, the same thing here, do I save the data to one or both of the two tables that has a many-to-many relationship? And how do I know when I should save to the bridge table?
Very confusing and I can't find any good tutorial with code examples that I can learn from. Since I created Controllers with read and write Views, I thought this matter was taken care of in the auto genrated code, but since the bridge table is empty despite that I have added data to the other tables, I need to ask this question to get some clarity about this! Preciate if any could tell me how it works with simple example or link. Thanks!
EDIT: Table Entities for some of the tables. I'm doing this project with ASP.NET MVC.
public class Order
{
public int ID { get; set; }
public string Name { get; set; }
public int? ManufacturerID { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int EmployeeNumber { get; set; }
public virtual ICollection<Timeunit> Timeunits { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
The above relationship will create a bridge table like:
public class OrderEmployeeEnrollments{
public int OrderId {get;set;}
public int EmployeeId {get;set;}
}
To Read data from Order table for an employee:
using (var context = new YourContext())
{
var order= context.Orders.Where(o => o.OrderEmployeeEnrollments.
Any(e => e.Employee.EmployeeId == employeeId );
}
To insert in Order table:
using (var context = new YourContext())
{
var order= new Order{
Name="",
ManufacturerID= 2 ,
Manufacturer = new Manufacturer { // create Manufacturer },
Employees = new List<Employee> { // create list}
}
context.Orders.Add(order);
context.SaveChanges();
}
To insert 'Order' table with an existing employee:
using (var context = new YourContext())
{
var order= new Order{
Name="",
ManufacturerID= 2 ,
Employees = context.Employees.Where(e=> e.Name=="Name")
}
context.Orders.Add(order);
context.SaveChanges();
}
Hope this will help :)
Entity Framework works in a way that you don't have to concern yourself with the bridge table. It handles that for you as long as you have defined the relationship between entities.
In this case:
public class Order
{
...
public virtual ICollection<Employee> Employees { get; set; }
}
public class Employee
{
...
public virtual ICollection<Order> Orders { get; set; }
}
Here's an example..
WRITE:
Order order = new Order();
order.Name = "foo";
order.Employees = _ctx.Employees.Where(e => id > 0 && id <= 5);
_ctx.Orders.Add(order);
_ctx.SaveChanges();
READ:
List<Employee> employees = _ctx.Employees.Where(e => id > 0 && id <= 5);
foreach(Employee emp in employees)
{
Order theOrder = emp.Orders.FirstOrDefault(o => o.ID == order.ID);
if(theOrder != null)
{
Console.WriteLine("Employee {0} has served order {1}", emp.ID, theOrder.ID);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With