ef——存储过程
数据库中存在存储过程GetCategory:
ALTER proc [dbo].[GetCategory] @cid int as begin select * from Categories where @cid = cid end
执行此存储过程的代码如下:
public IEnumerable<Category> GetCategoryWithProductsWithID(int id) { var parameter = new SqlParameter { DbType = DbType.Int32, ParameterName = "cid", Value = id }; //联表并延迟加载 var result = (from p in this.Categories.SqlQuery("EXECUTE GetCategory @cid", parameter) select p).ToList(); return result; } //参数是一一对应的
例如:
var country = "Australia"; var keyWords = "Beach, Sun"; var destinations = context.Database.SqlQuery<DestinationSummary>("dbo.GetDestinationSummary @p0, @p1", country, keyWords);