-1

I have tables with the same structure but with a letter prefix of every table.

For example:

A_Company, B_Company, C_Company

There is combo box from which the user can select A, B or C, and then the code saves data into the appropriate table.

How can I do this using EF database-first?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nurlan
  • 105
  • 1
  • 10
  • Do it has the same fields in tables? – hakantopuz Dec 10 '17 at 13:46
  • yes exactly same – Nurlan Dec 10 '17 at 13:47
  • This doesn't seem like a correct approach. If they all have a similar structure, why don't you save the records into a single table? Just add some property to discriminate upon, based on the combo box selection. – Bozhidar Stoyneff Dec 10 '17 at 13:48
  • There are some reasons doing this – Nurlan Dec 10 '17 at 13:51
  • You should the one table and add discriminator on the table.The method you are using is not correct. – hakantopuz Dec 10 '17 at 13:54
  • I know this is not correct approach but I have to do it with that way.This requirement of my company – Nurlan Dec 10 '17 at 13:59
  • OK. What do you mean by "dynamically"? are these table defined already in the database or you defining a new schema for every new list item in the combo box? – Bozhidar Stoyneff Dec 10 '17 at 14:24
  • Every table with prefix A, B, C etc. will be created by the end user. And then created letter prefix of the table must be added to the combo box. User must be able to select one of this letters and the user must be able to add update or delete rows from that table – Nurlan Dec 10 '17 at 14:32

2 Answers2

1

I solved this problem adding a column for code prefix and triggers on my base table company for insert update and delete.

Nurlan
  • 105
  • 1
  • 10
0

As the other commenters have said, it would be much better to refactor the database to a single table. If you can't do that then the only other thing that I can think of is to have a class which will select the table for you.

I would create a new class which has the same properties as your company tables, and also has the descriminator property. This would then be used as the data source for your ui.

in this class you would have to code manually to draw the data from the correct actual table (and save to it) based on the value of the discriminator. This is fine if you have only a few tables, but as your number of identical tables grows large, this will become more of a headache.

It might be possible to have the base tables all inherit from a virtual base class which would help a bit - you could then create a dictionary which the base class could use to switch the final data source on the fly.

As a final thought have you considered: 1. Creating the master table as suggested by the other commentators as a single table and then having views for each company.

  1. Creating the master table as suggested and then having code to create the individual tables from that one at some point prior to their use?
Tim
  • 120
  • 1
  • 3
  • 8
  • Thanks everyone for the help . I solved this problem adding a column for code prefix and triggers on my base table company for insert update and delete. – Nurlan Dec 12 '17 at 14:59