0

As I am working on a new platform (.net core 3 API) where every company has a separate database. I'm finding myself with an issue I can't find a solution to on the internet.

Companies have different users, but users should be able to login on different companies if they have the right to. Having a master database with all companies/users would work but then I would lose the foreign keys to the users? How is this issue solved by best practices?

I'm not talking about SQL users but users that are created on our application.

4
  • You can work with linked servers to keep your foreign keys intact. Commented Nov 28, 2019 at 11:01
  • @StevenLemmens that doesn't work with Entity Framework Core though. Commented Nov 28, 2019 at 11:04
  • @Jesse EF isn't the problem, it can work with three-part names or synonyms. The real problem is that cross-database foreign keys aren't allowed. You'll have to use triggers to handle validation. Commented Nov 28, 2019 at 11:10
  • Having a central authentication service isn't a bad idea - in fact, it's already available in the form of Identity Server. Even ASP.NET Identity uses (should use) a different model for users from the rest of the application. Commented Nov 28, 2019 at 11:14

1 Answer 1

1

Companies have different users, but users should be able to login on different companies if they have the right to. Having a master database with all companies/users would work but then I would lose the foreign keys to the users? How is this issue solved by best practices?

The best practice in a database-per-tenant solution is to maintain a central database with global configuration data (like this), and also replicate the relevant data to the tenant databases, as needed. So you should have a central database where you administer the relationship between users and companies, but then copy all of the users for each company into their company database.

Typically this reference data is slow moving, and so you can manage the copying by either writing to both databases directly from the application, or by running a batch job.

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.