Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

entity framework adding record to join table

Given 3 database tables: Businesses, Markets, BusinessesMarkets

I want to add a new business and I want the business to be in a particular market. How do I add to the join table in EntityFramework speak?

For example, the user wants to create a new business called ABC Co and add it to the Southern region market.

like image 527
Rod Avatar asked Feb 23 '23 22:02

Rod


1 Answers

This is making some assumptions about your domain model:

var southernRegion = db.Markets().Find(the_id);
var abcCo = new Business();

southernRegion.Businesses.Add(abcCo);

db.SaveChanges();

EF will handle the join table for you!

like image 110
Paul Avatar answered Mar 06 '23 23:03

Paul