使用EF 的简单的增删改查

时间:2023-03-09 07:55:29
使用EF 的简单的增删改查
using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace BLL
{
public class InfoManage
{
private BaseDal db;
public InfoManage(BaseDal dal)
{
this.db = dal;
} public InfoManage()
{
this.db = new BaseDal();
}
public bool Add(Info lst)
{
db.BeginTranscation();
db.SaveOrUpdate(lst, true);
return db.Commit() > ;
} public List<Info> GetList()
{
return db.GetEntity<Info>().ToList();
}
}
}

ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,早期被称为 ObjectSpace。

 1.搭建EF环境

  

   在DAL层添加项目ADO.NET 实体数据库模型

使用EF 的简单的增删改查

在Model层添加EF 5.x DbContext生成器

使用EF 的简单的增删改查

修改inputFile的值

使用EF 的简单的增删改查

DAL 代码

  

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks; namespace DAL
{
public class BaseDal : IDisposable
{
private TestEntities db = new TestEntities(); private bool isTransaction = false; public IQueryable<T> GetEntity<T>() where T : class
{
return db.Set<T>().AsNoTracking().AsQueryable<T>();
} /// <summary>
/// 添加
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="lst"></param>
/// <returns></returns>
public int Add<T>(List<T> lst, bool isCommit = true) where T : class
{
foreach (var item in lst)
{
db.Entry<T>(item).State = System.Data.EntityState.Added;
} if (isCommit && !isTransaction)
return db.SaveChanges();
else
return ; } /// <summary>
/// 根据条件删除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <returns></returns>
public bool DeleteByCondition<T>(Expression<Func<T, bool>> predicate, bool isCommit = true) where T : class
{
db.Set<T>().Where<T>(predicate).ToList<T>().ForEach(d => db.Entry<T>(d).State = System.Data.EntityState.Deleted);
if (isCommit && !isTransaction)
return db.SaveChanges() > ;
else
return false;
} public bool UpdateByCondition<T>(Action<T> updateExpression, Expression<Func<T, bool>> predicate, bool isCommit = true) where T : class
{
var lst = db.Set<T>().Where<T>(predicate).ToList<T>();
lst.ForEach(item =>
{
updateExpression(item);
db.Entry<T>(item).State = System.Data.EntityState.Modified;
}); if (isCommit && !isTransaction)
return db.SaveChanges() > ;
else
return false;
} public bool SaveOrUpdate<T>(T entity, bool isAdd = false, bool isCommit = true) where T : class
{
if (isAdd)
db.Set<T>().Add(entity);
else
db.Entry(entity).State = System.Data.EntityState.Modified; if (isCommit && !isTransaction)
return db.SaveChanges() > ;
else
return false; } public bool SaveOrUpdateForList<T>(List<T> entities, bool isAdd = false, bool isCommit = true) where T : class
{
foreach (T entity in entities)
{
if (isAdd)
db.Set<T>().Add(entity);
else
db.Entry(entity).State = System.Data.EntityState.Modified;
}
if (isCommit && !isTransaction)
return db.SaveChanges() > ;
else
return false;
} public int ExecuteSqlCommand(string sql, bool isCommit = true)
{
db.Database.ExecuteSqlCommand(sql);
if (isCommit && !isTransaction)
return db.SaveChanges();
else
return ;
} public int ExecuteSqlCommand(string sql, bool isCommit, params object[] parameters)
{
db.Database.ExecuteSqlCommand(sql, parameters);
if (isCommit && !isTransaction)
return db.SaveChanges();
return ;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ProcName"></param>
/// <param name="paramsStr"></param>
/// <returns></returns>
public List<T> ExecPro<T>(string ProcName, object[] paramsStr)
{
try
{
string sql = "exec " + ProcName; if (paramsStr.Length > )
{
string str = string.Empty;
for (int i = ; i < paramsStr.Length; i++)
{
if (str != string.Empty)
str += ","; str += "@p" + i.ToString();
}
sql += " " + str;
} return db.Database.SqlQuery<T>(sql, paramsStr).ToList();
}
catch (Exception ex)
{
return new List<T>();
}
} /// <summary>
/// 执行存储过程 无超时
/// </summary>
/// <param name="ProcName"></param>
/// <param name="parList"></param>
public void ExecPro(string ProcName, List<SqlParameter> parList)
{
DbConnection conn = db.Database.Connection;
if (conn.State == ConnectionState.Closed)
conn.Open();
DbCommand cmd = conn.CreateCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = ProcName;
cmd.CommandTimeout = ;
foreach (SqlParameter par in parList)
{
cmd.Parameters.Add(par);
} cmd.ExecuteNonQuery();
} public void BeginTranscation()
{
isTransaction = true;
} public int Commit()
{
if (isTransaction)
{
isTransaction = false;
return db.SaveChanges();
}
else
return ;
} /// <summary>
/// 执行SQL查询语句
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public List<T> FindEntityListBySql<T>(string sql)
{
return db.Database.SqlQuery<T>(sql).ToList<T>();
} public void Close()
{
if (db != null)
{
db.Dispose();
db = null;
}
} public void Dispose()
{
if (db != null)
{
db.Dispose();
db = null;
}
}
}
}

BLL 代码

 using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace BLL
{
public class InfoManage
{
private BaseDal db;
public InfoManage(BaseDal dal)
{
this.db = dal;
} public InfoManage()
{
this.db = new BaseDal();
} public bool Add(Info lst)
{
//db.BeginTranscation();
//db.SaveOrUpdate(lst, true);
//return db.Commit() > 0;
return db.SaveOrUpdate(lst, true, true);
} public bool Save(Info info)
{
return db.SaveOrUpdate(info, false, true);
} public bool Delete(Info info)
{
return db.DeleteByCondition<Info>(e => e.Name == info.Name, true);
} public List<Info> GetList()
{
return db.GetEntity<Info>().ToList();
}
}
}

客户端调用