原始的SQL查询和Entity Framework核心 [英] Raw SQL queries and Entity Framework Core

查看:125
本文介绍了原始的SQL查询和Entity Framework核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迁移我的应用程序的ASP.NET MVC的核心和Entity Framework的核心,我发现问题。我有原始的SQL查询,像这样的实体

I migrate my application to ASP.NET MVC Core and Entity Framework Core and i found problem. I have raw SQL query to entity like this

var rawSQL = dbContext.Database.SqlQuery<SomeModel>("Raw SQL Query").ToList();



但没有类SqlQuery< T> context.Database 。你有这个问题的解决方案。

But there is no SqlQuery<T> in context.Database. Do you have solution for this problem?

推荐答案

请确保您添加使用Microsoft.Data.Entity?;
因为你可以使用扩展方法。

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

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



甚至更好,而不是使用原始的SQL(在SQL注入攻击的风险)本FromSql方法允许你使用参数化查询,如:

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);

这篇关于原始的SQL查询和Entity Framework核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆