前一章讲解了如何配置DbSession,本章重点讲解利用MySoft.Data进行数据的插入操作
这里先引用一下上一章的DbSession的配置代码
DbSession配置
/// <summary>
/// 数据库访问类
/// </summary>
public static class DataAccess
{
/// <summary>
/// 通过配置节来实例化DbSession
/// </summary>
public static readonly DbSession DefaultSession = new DbSession("DataExample");
/// <summary>
/// 通过自定义类来实例化DbSession
/// </summary>
public static readonly DataExample ExampleSession = new DataExample();
}
/// <summary>
/// DataExample会话类
/// </summary>
public class DataExample : DbSession
{
public DataExample()
: base("DataExample")
{
#if DEBUG
this.RegisterSqlLogger(log =>
{
System.IO.File.WriteAllText("c:\\log.txt", log);
});
#endif
}
/// <summary>
/// 插入实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="item"></param>
/// <returns></returns>
public int Insert<T>(T item) where T : Entity
{
item.Detach();
return base.Save(item);
}
/// <summary>
/// 更新实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="item"></param>
/// <returns></returns>
public int Update<T>(T item) where T : Entity
{
item.Attach();
return base.Save(item);
}
}