Entity Framework code-first & stored procedure -
i using code-first approach in application. have generated entities (tables) using approach. create stored procedure through code. can guide me tried migration option , it's failing.
i using entity framework code first approach.using this,i have created customer , other entities.
now want create stored procedure 'getcustomers' using context class , pass parameters , result set in collection
it has return 2 collections below
create procedure getcustomer @name nvarchar(max),@zipcode int
as
select id,name,zipcode customer name (@name );
select id,name,zipcode customer zipcode =@zipcode
i want create stored procedure 'getcustomers' using context class , not manually execute in db.i need achieve below results:
1.pass name parameter alone , return first collection
2.pass zipcode parameter alone , return 2nd collection.
3.combine result collection of 1 , 2 single collection using merge
you can create/generate stored procedure using createstoredprocedure() method using add- migration option in entity framework.
step 1: generate migration script using add-migration sp_do_not_delete in package manager console. if no model changes there, system generate empty migration script below.
public partial class sp_do_not_delete : dbmigration { public override void up() { } public override void down() { } }
step 2: after generating script, please add stored procedure inside up() , down() methods below. note: below example, "dbo.getnextdisplayid" stored procedure name used nextavailabledisplayid using stored procedure.
public partial class sp_do_not_delete : dbmigration { public override void up() { createstoredprocedure( "dbo.getnextdisplayid", body: @"declare @requestid int select @requestid = nextavailabledisplayid [trackingnumberhistories] (tablockx) update [trackingnumberhistories] set nextavailabledisplayid = @requestid + 1 select @requestid 'nextavailabledisplayid'" ); } public override void down() { dropstoredprocedure("dbo.getnextdisplayid"); } }
note: createstoredprocedure() in up() method create stored procedure automatically whenever running migration script. dropstoredprocedure() in down() used drop stored procedure when ever roll back/delete stored procedure automatically in migration script.
hope might move forward!!
Comments
Post a Comment