Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Association of foreign keys between schemes or different databases #4595

Closed
rafaelluisdacostacoelho opened this issue Feb 18, 2016 · 6 comments

Comments

@rafaelluisdacostacoelho

The migrations not support the association of foreign keys between schemes or different databases. Are there any plans to address this issue or it will never be implemented? For example, I have a security module and want to reuse it for other applications, specifically associate the user with a different game profiles. I really need this.

Best regards

@eestein
Copy link

eestein commented Feb 18, 2016

+1
Inter db relation seems to be complicated... I'm not aware of how things are today, but back in 2011 I also needed to work with more than one database, here's the question just for reference:

http://stackoverflow.com/questions/5016753/associate-tables-from-different-databases-entity-framework

I hope this gets implemented if possible ;)

@rowanmiller
Copy link
Contributor

Do you want to just be able to query between databases on the same server (or linked databases) - which can be handled by database-qualifying the table names on commands sent to the single database. Or, do you want EF to handle connections between multiple database, splitting up of the queries, etc.

@rafaelluisdacostacoelho
Copy link
Author

The idea is basically this.

http://stackoverflow.com/questions/30608266/entity-framework-relationships-between-different-dbcontext-and-different-schemas

I really need execute this sql to create the foreign keys and constraints after creating my contexts using migrations, or exist other way?

The microsoft have plans to implement this automation with migrations?

Thanks

@rowanmiller
Copy link
Contributor

Sounds like I misunderstood what you were asking for 😄. So you have several contexts that model different parts of your database and you want to be able to have FK relationships between those different parts of the model? If so, the current workaround is to have one large model that covers everything - used only for migrations - and then several smaller contexts that your app uses for data access. But we do want to be able to handle this more elegantly - see #2725. If I'm misunderstanding again, just let me know 😄.

@rafaelluisdacostacoelho
Copy link
Author

Perfect, you understood correctly, the issue #2725 is just that. :D

I have a superficial idea, see, you can create a type of dependency between contexts, for example, you create a main context that will be executed by migrations, but this main context has dependencies with other contexts through foreign keys, then all you need is verify the integrity of these relationships, and publish only the main context, the other contexts will be used only to verify the integrity between the foreign keys.

For example:

public class Profile
{
    // From IdentityDbContext
    public virtual int UserId { get; set; }
    public virtual IdentityUser User { get; set; }

    // From RegisterDbContext
    public virtual int PeopleId { get; set; }
    public virtual People People { get; set; }
}

public class Dota2DbContext : DbContext
{
    // These are the contexts that this depends
    public DbDependency<IdentityDbContext> IdentityDbContext { get; set; }
    public DbDependency<RegisterDbContext> RegisterDbContext { get; set; }
}

Thanks. :)

@rowanmiller
Copy link
Contributor

Closing as we have the scenario covered by #2725

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants