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;

posted @ 2013-02-28 10:05  Yu  阅读(2976)  评论(1编辑  收藏  举报