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