James
James

Reputation: 2610

FK in one to many relationship code first

In model first, when we create a one to many relationship between two entities, there will be a FK auto-created in many end, when we do in code first, do we need to add a FK property in the many end entity? Why? Thx in advance!

Upvotes: 1

Views: 347

Answers (2)

Marthijn
Marthijn

Reputation: 3402

I once had some problems with relationships. It turned out I had to add the FK property otherwise EF couldn't see the relationship and track the changes, see my old question: How to update related entities in Entity Framework

Upvotes: 1

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364409

You don't need to add it. If you don't add it EF will generate it in database in the same way as it did it in model first. The question Why? refers to difference between foreign key and independent associations.

Upvotes: 0

Related Questions