Search code examples
c#sqldatabaseruntimebuilder

How to choose database table to update druing runtime


I am trying to update different tables in my db depending on which object a FactoryBuilder returns in my code. I want to use the InstrumentFactory which gives me the object/Instrument at runtime which I then do some operations with (updating info)

var tmpInstrument = InstrumentFactory.MakeInstrument(nameOfTable);
//old code was.....  var tmpInstrument = new SuperTable();

However, when I later want to update my DB I don't know how to code it as the object by the InstrumentFactory is set at runtime depening on "nameOfTable". I want to update different tables depending on what object tmpInstrument might be.

                    if (tmpInstrument is SuperTable)
                    {
                       _context.SupterTable.Add((ObjectCast)tmpInstrument);

                       _context.SaveChanges();

                    }

Is there any nice way to say update _context.ThisParticularTable or _context.AnotherObjectTable depending on which object the tmpSplitInfo is And also removing the if-statement and casting.

Thanks


Solution

  • This is what I was looking for (thanks Elias).

    _context.Set(tmpInstrument.GetType()).Add(tmpInstrument);