efef 使用存储过程程怎么用

3678人阅读
&1、执行返回数据列表型
& &&CREATE PROCEDURE [dbo].[SP_ManageForumGroupsPostRecycle]
@userId VARCHAR(128), &--用户ID
SET NOCOUNT ON;
DECLARE @rowId INT&
SET @rowId=( @page - 1 ) * @pageS
----列表数据
G.Id,P.Title,G.PostId,A.NikeName,G.CreateDate,G.Note FROM GroupsPostRecycle AS G&
LEFT JOIN dbo.GroupsPost AS P ON g.PostId=p.Id
LEFT JOIN HTUser.dbo.AspNetUsers AS A ON A.Id=G.UserId
WHERE 1=1 And
& ( @userId IS NULL &ORp.GroupId
IN(SELECT id FROM dbo.Groups WHERE G.UserId=@userId))
ORDER BY G.CreateDate DESC&
************************************************************************************************************************************************************&
执行存储过程的方法
& & & & & & & & & & DBContext&db=new &DBContext&();
& & & & & & & & & & SqlParameter[] sqlParms = new SqlParameter[1];
& & & & & & & & & & sqlParms[0] = new SqlParameter(&@userId&,
& &//结果集& & & & & & &
var result=(from &G in db.GroupsPostRecycles.sqlQuery(&exec&SP_ManageForumGroupsPostRecycle&@userId&,sqlParms&)select
G).tolist();
2、返回值
Create proc SP_GetPostCount&
&@type int&
& select count(id) from post where type=@type
**********************************************************************************************
& DBContext&db=new &DBContext&();
& & &SqlParameter[] sqlParms = new SqlParameter[1];
& & &sqlParms[0] = new SqlParameter(&@type&,&type);
& & & &Type t=typeof(int);
& & & var result=db.Database.SqlQuery(t,&exec&SP_GetPostCount&@type&,sqlParms&).Cast&int&().First();
3、执行增删改
IF OBJECT_ID('SP_SendSystemMessageAndGroupOper') IS NOT NULL&
DROP PROCEDURE SP_SendSystemMessageAndGroupOper
CREATE PROC SP_SendSystemMessageAndGroupOper
&@Ids VARCHAR(4000),
&@userid VARCHAR(225),
&@operType INT,
&@msg VARCHAR(255)
SET NOCOUNT ON;
DECLARE @sql VARCHAR(1000)
--插入日志
SET @sql='INSERT INTO dbo.GroupsOperLog(Title ,UserId ,GroupsID ,CreateDate , Content ,OperType)SELECT ''圈子''+GroupsName+'''+@msg+''','''+@userid+''',ID,GETDATE(),''圈子''+GroupsName+'''+@msg+''','+CONVERT(VARCHAR(10),@operType)+'
FROM dbo.Groups WHERE id IN('+@ids+') AND UserId='''+@userid+''' AND [State]=0'
EXEC(@sql)
--插入系统消息
SET @sql='INSERT INTO dbo.MessageForSystem(Id ,UserId ,Title ,Content ,IsRead ,CreateDate)SELECT NEWID() ,G.UserId, ''圈子''+G1.GroupsName+'''+@msg+''', ''圈子''+G1.GroupsName+'''+@msg+''',0,getDATE() FROM dbo.GroupMembers AS
G &LEFT JOIN dbo.Groups AS G1 ON G1.ID = G.GroupId WHERE G.GroupId IN('+@Ids+') and (G.ApplyState=1 or G.ApplyState=4)'
& EXEC(@sql) &&
*************************************************************************************************************************************************************
& SqlParameter[] sqlParms = new SqlParameter[4];
& sqlParms[0] = new SqlParameter(&@Ids&, strid);
& sqlParms[1] = new SqlParameter(&@userid&, userid);
& &sqlParms[2] = new SqlParameter(&@operType&, &0&);
& sqlParms[3] = new SqlParameter(&@msg&, &已解散&);
& &int count=await db.Database.ExecuteSqlCommandAsync(&exec SP_SendSystemMessageAndGroupOper @Ids,@userid,@operType,@msg&, sqlParms);&1、无参数查询var&model&= db.Database.SqlQuery&UserInfo&("select* from UserInfoes ").ToList();
2、有参查询var model&= db.Database.SqlQuery&UserInfo&("select* from UserInfoes where id=@ID ",new SqlParameter("@ID",id)).ToList();3、结合linq查询
var model&= (from p in db.userinfo.SqlQuery("select * from
UserInfoes where id=@ID", new SqlParameter("@ID", 1)) select
p).ToList();4:EF原始查询单独表
model = db.userinfo.OrderByDescending(a =& a.ID).Skip(10 * (当前页 - 1)).Take(10).ToList();
5,EF 执行存储过程或delete 删除语句
SqlParameter[] para = new SqlParameter[] {
new SqlParameter("@ID",id)
db.Database.ExecuteSqlCommand("delete UserInfoes &where id=@ID", para);存储过程 删除,有几个参数,存储后面要带几个参数以逗号分隔
SqlParameter[] para = new SqlParameter[] {
new SqlParameter("@ID",id)
db.Database.ExecuteSqlCommand("sp_Userinfos_deleteByID @ID", para);
阅读(...) 评论()Where can I get good tutorial on Entity framework with Stored Procedure in MVC framework?
Is it better to use Enterprise library in this case when I have almost everything written in the stored procedure.
Note: I am using stored procedure because they are really very complex and some of them is over 1000 lines.
解决方案 MVC is in this case absolutely not related. The way how you call stored procedure from EF will be still the same. I guess you want to use stored procedures without actually using entities and linq-to-entities (main EF features), don't you? Generally you need:
EDMX file (ado.net entity data model) where you run update from database and add all stored procedures you want to use. EDMX file also generates derived ObjectContext and all entities by default.
Next you must go to
for each procedure. Function import will create method on the derived ObjectContext which will allow you call the stored procedure as any other .net method.
During function import you will have to create complex type (it can happen automatically) for result set returned from stored procedure.
You also don't have to use function imports at all and
by calling either:
objectContext.ExecuteSqlCommand("storedProcedureName", SqlParameters) for SPs not returning record set
objectContext.ExecuteStoreQuery&ResultType&("storedProcedureName", SqlParameters) for SPs returning record set. ResultType must have properties with same names as columns in result set. It can work only with flat types (no nested objects).
There are some limitations when using stored procedures:
Entity framework doesn't like stored procedures which returns dynamic result sets (based on some condition result set has different columns)
Entity framework doesn't support stored procedures returning multiple result sets - there are
which does but it is more like doing ADO.NET directly.
本文地址: &
我在哪里可以得到实体框架很好的教程是存储过程的MVC框架?它是更好的时候我几乎都写在存储过程中这种情况下,使用企业库。请注意:我使用的存储过程,因为他们真的很复杂,其中有些是超过1000行解决方案
MVC在这种情况下,绝对没有关系的。你打电话是如何从EF存储过程的方式将仍然是相同的。我猜你想使用存储过程实际上并没有使用实体和LINQ到实体(主要EF功能),不是吗?一般来说,你需要:你来自哪里,数据库运行更新,并添加您要使用的所有存储过程EDMX文件(ado.net实体数据模型)。 EDMX文件也产生衍生的ObjectContext ,默认情况下所有实体。接下来你必须去。功能导入将创建方法派生的ObjectContext 这将允许您调用存储过程与任何其他.NET方法。在功能导入你必须创建复杂的类型从存储过程返回的结果集(它可以自动发生)。您也不必使用功能的进口都和这不,但它更像是直接做ADO.NET。
本文地址: &
扫一扫关注官方微信其他回答(2)
EF完全可以胜任,具体性能数据没有。51cto前期就是EF框架。所以你的需求肯定能满足。
收获园豆:1
SQL在控制力度方面肯定比EF要好点,但是EF性能用的好也不会差,注意我说是用的好哦,借用二楼的话,通常EF的性能问题一般是程序员造成的,不是微软造成的
收获园豆:1
清除回答草稿
&&&您需要以后才能回答,未注册用户请先。在Entity Framework中使用存储过程_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
在Entity Framework中使用存储过程
&&介绍详细,收集博客资料整合而成,仅供学习
阅读已结束,下载本文需要
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩29页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢

我要回帖

更多关于 ef 调用存储过程 的文章

 

随机推荐