EF常用的添加和修改数据

if (typeof(T) == typeof(PLC_CommunicationData) || typeof(T) == typeof(PLCDeviceInfo))
{
  using (var context = new TuniSettingDataContext())
  {

    if (Exist<T>(entity))
    {
      context.Entry<T>(entity).State = EntityState.Modified;
    }
    else
    {
      context.Entry<T>(entity).State = EntityState.Added;
    }
    return context.SaveChanges() > 0;
  }
}

posted @ 2022-02-28 09:47  MaxBruce  阅读(402)  评论(0)    收藏  举报