2

We are using entity framework code first to save reports to SQL database, many of the objects have many to many relations so the data is split into different tables. In order to prevent duplication of data we first check if a certain object is already saved and later on we add the relation to the database.

For example have object Person that can have many countries, and object Country that can hold multiple Person object.

During the beginning of the save flow we query the database for existing countries and update them in the Person object if they exist, or create them if they don't.

This flow worked fine while we have only one saving process at the same time but now we got requirement to support it many times simultaneously and my worry is that one thread will add a new country right after other thread will check existing countries.

I was wondering what good practices are there to solve this problem with minimal impact on performance.

Thanks!

shlomie
  • 21
  • 1

1 Answers1

1

It doesn't sound like your fully leveraging the capabilities of your chosen ORM. Relationships are represented in your returned entities if you are using the library according to it's documentation. So updating a single entity's many-to-many relationship would update for all other related entities so long as the EntityID remains the same.

If you still are having trouble trusting the integrity of this relationship, I would suggest using the bulk-update feature of entity framework

Oxymoron
  • 1,380
  • 2
  • 19
  • 47