-1

I have multiple Classes: Student, Task, Todo and Subject. The Student is saved in a different Dbcontext then everything else, because the Student is my ASP.Net Identity User Class and I didn't want to mix Identity with Data. Generally the Student relates to multiple Tasks, every Task to a Todo and every Todo to a Subject. When I apply the migration now, New tables are created for Task, Todo and Subject altough there are already existing Tables just created by another Context.
My question now: How do I tell the first Context that the Tables already exist, but were created by the second Context(both use the same Database). I have already searched StackOverflow and the WWW but couldn't find an answer I understood/that would work for me.
Thanks in advance!

LuisTheOne
  • 11
  • 6
  • Have you checked this? https://stackoverflow.com/questions/11197754/entity-framework-one-database-multiple-dbcontexts-is-this-a-bad-idea – szmp Jun 03 '23 at 19:58
  • Since I don't want to create another context just for setting up the Database I thought about just putting everything into the same one. Do you think it is a bad idea to use the identity context for data too? – LuisTheOne Jun 05 '23 at 16:24
  • IMHO just use one context. Though you can hide the implementation behind an interface to ensure your services can't mess around with your identities. – Jeremy Lakeman Jun 06 '23 at 05:34
  • Strange. You can have 2 contexts using the same ConnectionString and everything "should just work". Post code for the configurations. – H H Jun 06 '23 at 05:34
  • You can exclude tables from being migrated https://jaliyaudagedara.blogspot.com/2021/01/ef-core-50-excluding-tables-from.html. IMHO it's annoying to handle the migration assemblies. – Jeremy Lakeman Jun 06 '23 at 05:38

1 Answers1

-1

After researching a bit longer I have come to the conclusion that it is the easiest to just use one context that inherits from IdentityDbContext.

LuisTheOne
  • 11
  • 6
  • It's better to use the ASP.Net Identity moel as-is and create a Student class in your business model. This Student class takes the ASP.Net Identity User PK as a foreign key (only in the database, not in the class model). – Gert Arnold Jun 06 '23 at 06:16
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 06 '23 at 07:08