0

I have two entities, a project and a company. One company has a list of multiple projects and a project has one company.

At this time I'm adding a existing company from the DB to project

e.g.

project.company = getCompanyByName("Microsoft");

Which returns the proper company and does add it to my project.

However, when I add this new project to the DB as such:

ctx.AddToProjects(project);
ctx.SaveChanges();

It will also add a second Microsoft company So how do I make my project refer to the existing company without adding it, again?

1 Answer 1

1

Attach the Company to the context before you associate it with a Project.

Once the Company is safely in the context, actions that you perform on the Projects won't affect its state.

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

Comments

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.