c# - How to call stored procedure using Entity Framework code-first? -
i want create delete user userid using stored procedure in entity framework code-first. new entity framework don't know how can task. can please let me know how can that?
this code far:
stored procedure in sql server:
create procedure [dbo].[deleteuserbyid] @userid bigint begin delete [dbo].[users] id = @userid end
now can stored procedure used in code? don't know 1 please tell me.
this class model builder :
public class applicationdbcontext : identitydbcontext<applicationuser> { public applicationdbcontext() : base("defaultconnection") { } public virtual dbset<userinfo> userinfo { get; set; } public static applicationdbcontext create() { return new applicationdbcontext(); } public virtual objectresult<list<users>> getusers() { return ((iobjectcontextadapter)this).objectcontext.executefunction<list<userinfo>>("getusers"); } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<users>() .maptostoredprocedures(s => s.delete(u => u.hasname("deleteuserbyid", "dbo") .parameter(b => b.id, "userid")) ); } }
this delete method in controller :
public string deleteusers(int id) { //here how can call sp , delete user }
if knows, please me task.
you can executesqlcommand method on database.
this.database.executesqlcommand( "[dbo].[deleteuserbyid] @userid", new sqlparameter("@userid", id));
update
(answer comment)
[table("users")] public class applicationuser { }
Comments
Post a Comment