Answered by:
Using Stored Procedure with EF Code First

Question
-
User-1165436258 posted
Hi,
I'm using EF 6x Code First. I want to use and run some of my queries with SQL Server stored procedures in order to get faster speed.
But issue is that how can I develop it. I'm also using database migration. When I want to generate database with EF code first, how to create stored procedures (or database functions) automatically?
Tuesday, August 23, 2016 5:10 AM
Answers
-
User283571144 posted
Hi ali_shmki3,
But issue is that how can I develop it. I'm also using database migration. When I want to generate database with EF code first, how to create stored procedures (or database functions) automatically?As far as I know, the DbMigration class has methods such as "CreateProcedure" which allow for modification of stored procedures.
I suggest you could override the OnModelCreating method in DbContext class to call MapToStoredProcedures method.
More details, you could refer to follow codes and link:
public partial class MappedSPtoProductEntity : DbMigration { public override void Up() { CreateStoredProcedure( "dbo.Product_Insert", p => new { Name = p.String(), Price = p.Decimal(precision: 18, scale: 2), }, body: @"INSERT [dbo].[Products]([Name], [Price]) VALUES (@Name, @Price) DECLARE @Id int SELECT @Id = [Id] FROM [dbo].[Products] WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity() SELECT t0.[Id] FROM [dbo].[Products] AS t0 WHERE @@ROWCOUNT > 0 AND t0.[Id] = @Id" ); } public override void Down() { DropStoredProcedure("dbo.Product_Insert"); } }
DbContext Code:
class DataContext : DbContext { public DbSet<Product> Products { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Product>().MapToStoredProcedures(); base.OnModelCreating(modelBuilder); } }
Link:
http://www.dotnetodyssey.com/2015/03/12/calling-stored-procedure-from-entity-framework-6-code-first/
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 23, 2016 12:52 PM
All replies
-
User702547207 posted
Hi,
You can refer the below article for the same.
Tuesday, August 23, 2016 5:53 AM -
User283571144 posted
Hi ali_shmki3,
But issue is that how can I develop it. I'm also using database migration. When I want to generate database with EF code first, how to create stored procedures (or database functions) automatically?As far as I know, the DbMigration class has methods such as "CreateProcedure" which allow for modification of stored procedures.
I suggest you could override the OnModelCreating method in DbContext class to call MapToStoredProcedures method.
More details, you could refer to follow codes and link:
public partial class MappedSPtoProductEntity : DbMigration { public override void Up() { CreateStoredProcedure( "dbo.Product_Insert", p => new { Name = p.String(), Price = p.Decimal(precision: 18, scale: 2), }, body: @"INSERT [dbo].[Products]([Name], [Price]) VALUES (@Name, @Price) DECLARE @Id int SELECT @Id = [Id] FROM [dbo].[Products] WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity() SELECT t0.[Id] FROM [dbo].[Products] AS t0 WHERE @@ROWCOUNT > 0 AND t0.[Id] = @Id" ); } public override void Down() { DropStoredProcedure("dbo.Product_Insert"); } }
DbContext Code:
class DataContext : DbContext { public DbSet<Product> Products { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Product>().MapToStoredProcedures(); base.OnModelCreating(modelBuilder); } }
Link:
http://www.dotnetodyssey.com/2015/03/12/calling-stored-procedure-from-entity-framework-6-code-first/
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 23, 2016 12:52 PM