#region 新增
/// <summary>
/// 添加一条数据
/// </summary>
/// <param name="model">添加的对象</param>
/// <returns>影响行数</returns>
public int InsertByModel(T model)
{
db.Set<T>().Add(model);
return db.SaveChanges();
}
/// <summary>
/// 添加多条数据
/// </summary>
/// <param name="modelList">添加的对象的集合</param>
/// <returns>影响行数</returns>
public int InsertByList(List<T> modelList)
{
db.Set<T>().AddRange(modelList);
return db.SaveChanges();
}
#endregion
新增是最简单的,EF已经帮我们封装好了添加一个对象,以及一个集合的方法
接下来让我i们才测试一下
static void Main(string[] args)
{
try
{
MT5RiskManagementEntities mt5 = new MT5RiskManagementEntities();
EntityFramework<User> ef = new EntityFramework<User>(mt5);
int count = 0;
count = ef.InsertByModel(new User() { name = "马云", age = 53 });
Console.WriteLine(string.Format("新增了{0}条数据", count));
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
static void Main(string[] args)
{
try
{
MT5RiskManagementEntities mt5 = new MT5RiskManagementEntities();
EntityFramework<User> ef = new EntityFramework<User>(mt5);
int count = 0;
List<User> users = new List<User>();
for (int i = 0; i < 100; i++)
{
users.Add(new User() { name = "测试" + i.ToString("D5"), age = 20 + i % 5 });
}
count = ef.InsertByList(users);
Console.WriteLine(string.Format("新增了{0}条数据", count));
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}