I have two tables, Users and TempUsers and I need to do operations on both of them. I already have my users type defined and I want to add it to the DbContext for both tables. Problem is, it either uses convention to map the type name to a table or using the TableAttribute with the table name specified. Either way I can’t see how to add two dbsets mapping the type to different table names.
I could duplicate the type using either copy + paste or through a UserBase class and two derived User and TempUser classes. Both ways will work but really in the code I want to deal with Users and not have the complexity of Users and TempUsers in the code. After all it’s the repository’s responsibly to deal with where to put the user objects and the business logic shouldn’t have to deal with it.
Advice would be much appreciated. Thanks!
[Explanation Based On Comments]
The reason I have two tables is because the TempUsers is to support a bulk import/update but though atomic transactions on each user. So externally some active directory export or some such will result in calling a service for each user. I have to create/update users and figure out what ones are not being imported but already exist in my database and then delete them. Would be much simpler to truncate the Users table and write directly to that but the Id’s would be different and it would break all the links the users have to different tables, like shipping history for example.
That is not possible. EF cannot map same class twice within same context. Your single
Userclass can be only mapped toUserstable orTempUserstable in single context type. You need either two user classes or two different context types (with different mapping configuration) – one providing access toUserstable and second providing access toTempUserstable.