@manhng

Welcome to my blog!

EFCore-SQL

October 6, 2017 00:59

http://www.learnentityframeworkcore.com/raw-sql

https://elanderson.net/2016/04/execute-raw-sql-in-entity-framework-core/

https://docs.microsoft.com/en-us/ef/core/querying/raw-sql

https://dotnetthoughts.net/how-to-execute-storedprocedure-in-ef-core/

http://www.talkingdotnet.com/how-to-execute-stored-procedure-in-entity-framework-core/

https://www.sinclairinat0r.com/2017/05/06/entity-framework-core--mapping-stored-procedures,-fluently

 

EFCore-FluentStoredProcedure

https://github.com/snickler/EFCore-FluentStoredProcedure

 

Make sure you add using Microsoft.Data.Entity; because there is an extension method you could use.

var rawSQL = dbContext.SomeModels.FromSql("your SQL");

Even better, instead using raw SQL (at risk of SQL injections attacks) this FromSql method allows you to use parameterized queries like:

dbContext.SomeModels.FromSql("SELECT * FROM dbo.Blogs WHERE Name = @p0", blogName);

 

In EF Core you no longer can execute "free" raw sql. You are required to define a POCO class and a DbSet for that class. In your case you will need to define Rank:

var ranks = DbContext.Ranks
   .FromSql("SQL_SCRIPT OR STORED_PROCEDURE @p0,@p1,...etc", parameters)
   .AsNoTracking().ToList();

As it will be surely readonly it will be useful to include the .AsNoTracking() call.

 

What's New in Entity Framework Core 2.0
• EF Core now targets the new .NET Standard 2.0
• Improved LINQ translation
• Owned entities and Table Splitting
• Global query filters
• DbContext Pooling
• String interpolation in raw SQL methods

Categories

Recent posts