【发布时间】:2020-03-17 11:22:35
【问题描述】:
我正在尝试根据 FactoryBuilder 在我的代码中返回的对象来更新我的数据库中的不同表。 我想使用 InstrumentFactory,它在运行时给我对象/仪器,然后我用(更新信息)做一些操作
var tmpInstrument = InstrumentFactory.MakeInstrument(nameOfTable);
//old code was..... var tmpInstrument = new SuperTable();
但是,当我以后想更新我的数据库时,我不知道如何对其进行编码,因为 InstrumentFactory 的对象是在运行时根据“nameOfTable”设置的。我想根据 tmpInstrument 可能是什么对象来更新不同的表。
if (tmpInstrument is SuperTable)
{
_context.SupterTable.Add((ObjectCast)tmpInstrument);
_context.SaveChanges();
}
有什么好办法说更新 _context.ThisParticularTable 或 _context.AnotherObjectTable 取决于 tmpSplitInfo 是哪个对象 并且还删除了 if 语句和强制转换。
谢谢
【问题讨论】:
标签: c# sql database runtime builder