I have to create a solution in Asp.net core and I have to use a stored procedure based approach. So is it feasible to use Entity Framework core for the application? Because data return from the stored procedure, not an object declared in Entity Core
2 Answers
EF Core provides the following methods to execute a stored procedure:
DbSet<TEntity>.FromSql()
DbContext.Database.ExecuteSqlCommand()
var data = context.DbSet<TEntity>.FromSql("SPName").ToList();
var rowsAffected = context.Database.ExecuteSqlCommand("sql command");
For more details : entityframeworktutorial
and for use fields that not exist in model you can declare these in model and use NotMapped
attribute. NotMapped Attribute
And for mapping stored procedures, fluently look at this link
-
Thank you for an answer, Yes, I know I can do that, but it will return table specific entity. Sep 24, 2018 at 6:09
-
@Hrishikesh for mapping stored procedures look this solution : link Sep 24, 2018 at 6:36
It is important to understand what is needed. Entity Framework & using normal SQl command, both has it's pros & cons.
Basic points are :
Entity Framework
Can act as ORM for different databases, if you application uses two or more databases like SQL server & oracle. Almost same code can be used. Also it is easier to switch from one db to another.
EF has both code first & db first, also maintains connections & query generation so less concern for SQL injection & faster changes
Using EF however if your team has many developers who are committing EF changes frequently it is hard to merge internal layers code conflicts for EF.
If you have strong middleware & caching, EF can be optimised for application caching.
SQL Command object
More control over SQL since query generated in EF are at runtime so hard to analyse & optimize indices, & execution plan.
Need to take care for sql injections, parameter sniffing issues.
No issues for code merges till DB design is done with consultation in large Dev Teams.
SQL Command objects are easier to debug & analyse in maintenance with less effort in change since you need not to update whole EF files & related code.
Teams with dedicated DBA & large server for DB choose SQL command, Teams with small agile team with full stack dev with strong middleware usually go for EF.
Both of them are good for some use cases & none of them is silver bullet for all. so pick what suits you best.
-
We mostly concerned about the performance of application related to execution. So what would be your suggestion? Sep 24, 2018 at 7:49
-
Depends on your schema, number of reads, concurrent users/connections, amount of data read in requests, joins/indexes required, frequency of insert/update/deletion, lot of more factors. In general, for disconnected architecture with complex joins & concurrent users, you have more control via SQL command since you have more control over transactions, locks, read write & can optimize query plan better Sep 24, 2018 at 8:39
-
If you have more reads, less update/inserts, less complex transactions, joins & have good middleware caching EF can also out perform . Sep 24, 2018 at 8:42