在我的应用程序中,我创建了一个 userControl,然后我需要将它保存到数据库中。这是我的方法,我正在保存我的 userControl :
private void SaveControl(byte userControlType, ISelectableViewModel viewModel, Guid guid)
{
var context = new EntitiesNew();
var instrumentSettings = new INSTRUMENTSETTINGS
{
USERCONTROL = userControlType,
INSTRUMENTHEIGHT = (float)viewModel.InstrumentHeight,
INSTRUMENTWIDTH = (float)viewModel.InstrumentWidth,
INSTRUMENTSCALABLEVALUE = viewModel.ScalableValue,
INSTRUMENTPOSTOLEFT = (float)viewModel.InstrumentPosToLeft,
INSTRUMENTPOSTOTOP = (float)viewModel.InstrumentPosToTop,
INSTRUMENTZINDEX = viewModel.ZIndex,
CREATED_BY= guid
};
context.INSTRUMENTSETTINGSs.Add(instrumentSettings);
context.SaveChanges();
viewModel.ControlId = instrumentSettings.INSTRUMENTSETTINGSID;
var roomId = (from room in context.ROOMs
where room.ROOMNAME == viewModel.RoomName
select room.ROOMID).FirstOrDefault();
var roomWithInstrument = new ROOMWITHINSTRUMENT
{
CREATED_BY = guid,
INSTRUMENTSETTINGSID = viewModel.ControlId,
ROOMID = roomId
};
context.ROOMWITHINSTRUMENTs.Add(roomWithInstrument);
context.SaveChanges();
}
没有错误,它正在工作。唯一的问题是,当我第一次将记录保存到数据库时,它需要的时间太长(比如 2 秒)。第一次保存后效果更好(不到一秒钟)。所以我的问题是 - 我怎样才能提高第一次储蓄,所以它会节省得更快?