private void btnAdd_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "文件(*.*)|*.*"; if (dlg.ShowDialog() == DialogResult.OK) { var attach = new TA_Attach() { TableName = TableName, TablePKID = TablePKID, CreateDate = DateTime.Now, CreatorName = GlobalVar.Oper.OperName, FileName = Path.GetFileName(dlg.FileName) }; FileStream fs = File.OpenRead(dlg.FileName); fs.Position = 0; byte[] blobdata = new Byte[fs.Length]; fs.Read(blobdata, 0, (int)fs.Length); attach.FileData = blobdata; fs.Close(); SpareEntities db = EntitiesFactory.CreateSpareInstance(); AttachController.AddOrUpdate(db, attach, GlobalVar.Oper); EntitiesFactory.SaveDb(db); MessageHelper.ShowInfo("保存成功!"); RefreshData(); } }
public static void AddOrUpdate(SpareEntities db, TA_Attach selectedData, TS_OPERATOR oper) { var logType = db.TA_Attach.Any(p => p.UID == selectedData.UID) ? LogType.BaseDataModify : LogType.BaseDataCreate; try { db.TA_Attach.AddOrUpdate(p => p.UID, selectedData); } catch (DbEntityValidationException dbEx) { Console.WriteLine(dbEx.ToString()); throw; } }
public static void Delete(SpareEntities db, TA_Attach data, TS_OPERATOR oper) { db.Entry(data).State = EntityState.Deleted; }