0

I have a MVC .Net Core 3.1 project with 2 contexts. One is the standard Identity context and the other is the application context. These are stored as 2 separate databases on the server.

In my application I have an entity 'Project' and it has a field 'ProjectLeadUserId' in which I store the ID of that user from AspNetUsers.

When I list all of the open projects I would like to show the 'UserName' from AspNetUsers, rather than the GUID related to each of the projects.

Normally I would have a foreign key between the two so that in my view I could do something like:

project.ProjectLead.UserName

But I cant workout how to set that up in my entity as the FK would be in a different context.

Is this possible, or have I caused myself an issue by separating the Identity and application contexts?

Edit: Ignoring the foreign key issue as I can deal with that in the application logic. Is there a way to load the related data when multiple contexts are involved. e.g. When I get a list of projects, can I load the related AspNetUsers data, using the AspNetUsers.Id and Project.ProjectLeadUserId?

1 Answer 1

1

Quoting from SQL Server documentation:

FOREIGN KEY constraints can reference only tables within the same database on the same server. Cross-database referential integrity must be implemented through triggers. For more information, see CREATE TRIGGER.

It's not possible for you to have a foreign key across different databases in SQL Server.

If the 2 contexts targeted the same DB, I think what you want would still not be possible.

In my past projects I've usually seen people extending the IdentityDbContext to create their DB context and using only that.

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.