EF 之 存储过程


SqlParameter[] parms = new SqlParameter[2];

SqlParameter id = new SqlParameter("ID", "11a44466-6d9c-4c7e-b9e4-5a1369061119");

id.SqlDbType=SqlDbType.NVarChar;

id.Size = 128;

parms[0] = id;               

var outParam = new SqlParameter();

outParam.ParameterName = "Name";

outParam.SqlDbType = SqlDbType.VarChar;

outParam.Size = 64; //必须填写正确,否则会有这种异常:{"String[1]: the Size property has an invalid size of 0."}

outParam.Direction = ParameterDirection.Output;

parms[1]=outParam;

 

//当有这样异常时,"... A member of the type ... does not have a corresponding column in the data reader with the same name"

//是存储过程返回的字段名与对应Model的属性名对不上, 解决加法是在存储过程中取别名与Model的属性同名

var result = db.Database.SqlQuery<Category>("P_GetCategoryByID @ID,@Name output", parms);  

 

string title = outParam.Value as string;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM