ef 原生sql
查询
1.在context有实体对应的原生sql
如果执行的原生sql查询结果对应一个实体,可以调用FromSqlInterpolated
FormattableString sql =$"xxx"; context.xxx.FromSqlInterpolated(sql);
限制:
- context里要有这个dbset类,只能返回单一的实体类
- 只能写单个sql不能嵌套?
2.任意原生sql
使用了ado.net
//获得con var connection = sysDb.Database.GetDbConnection(); if(connection.State != System.Data.ConnectionState.Open) { connection.Open(); } using (var cmd= connection.CreateCommand()) { cmd.CommandText = sql; using (var reader=cmd.ExecuteReader()) { while (reader.Read()) { string module_code = reader.GetString(0); string module_name = reader.GetString(1); string is_menu = reader.GetString(2); } } }