4

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.

1 Answer 1

8

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Very cool! one thing: southernGroup.Markets.Add(abcCo), I didn't have .Markets in intellisense. I had southernGroup.Businesses? The tooltip showed "Market.Businesses"
I just wanted to say, "thanks!" again. This was an epiphany for me. Can't believe this was 4 years ago! I was reminded of this because I was hopefully able to help someone today with this knowledge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.