时间:2021-07-01 10:21:17 帮助过:16人阅读
//for xml query var resovle = new XmlResovle(); resovle.Load(@"E:\SqlBatis\SqlBatis.Test", "*.xml"); //普通sql var sql1 = resovle.Resolve("namespace.id"); //动态sql var sql2 = resovle.Resolve("namespace.id",new { Id=(int?)null,Age=2});
public class MysqlDbConrext : DbContext { public IDbQuery<Student> Students { get => new DbQuery<Student>(this); } private static readonly IXmlResovle resovle; static MysqlDbConrext() {
//不要为每一个DbContext加载配置 resovle = new XmlResovle(); resovle.Load(@"E:\SqlBatis\SqlBatis.Test","*.xml"); } protected override void OnLogging(string message, object parameter = null, int? commandTimeout = null, CommandType? commandType = null) { } protected override DbContextBuilder OnConfiguring(DbContextBuilder builder) { ILoggerFactory factory = LoggerFactory.Create(b => { b.AddConsole(); b.AddDebug(); b.SetMinimumLevel(LogLevel.Debug); }); builder.Connection = new MySql.Data.MySqlClient.MySqlConnection("server=127.0.0.1;user id=root;password=1024;database=test;"); builder.XmlResovle = null; return builder; } public MysqlDbConrext() { Students = new DbQuery<Student>(this); } }
var db = new DbContext(new DbContextBuilder() { DbContextType=DbContextType.Mysql, ... });
using (var db = new SqlDbContext()) { //linq var row = db.Students.Delete(); var (list, count) = db.Students .Page(1, 2) .SelectMany(); //dynamic var p = new { Id = (int?)null, Name = "zs" }; var dylist = db.Students .Where(a => a.Id == 1, p.Id != null) .Where(a => a.Name == p.Name, p.Name != null) .SelectMany(); //xml row = db.From("sutdent.add", new Student() { Age = 20, IsDelete = true, Name = "xml" }).ExecuteNonQuery(); //dynamic:注意由于内部会缓存表达式创建的委托,因此同一个动态的xml命令不能用不同的参数去获取它 //建议如果获取的xml命令是动态的,则应保证整个项目只有一次获取的代码,或者使用同一种参数类型 row = db.From("stutdent.list-dynamic", p).ExecuteNonQuery(); //多结果集查询,xml方式同样支持 using (var muti = db.ExecuteMultiQuery("select * from student;select count(1) from student;")) { //第一个结果集 var list = muti.GetList<Student>(); //第二个结果集 var count = muti.Get<int>(); } }
net下的半自动orm+linq的《SqlBatis》
标签:roo 性能 tde root lin highlight using 数据 enc