如何在实体框架中发送常规内联SQL

发布于 2024-10-16 06:46:30 字数 173 浏览 1 评论 0原文

现在先别生气。这仅适用于非常具体的情况。因此,我们不要问为什么我想通过 EF 发送内联字符串 SQL,而是尝试停留在“如何”的主题上。

我是否需要使用常规的旧 ADO.NET 按照老式路线执行此操作,或者 EF 是否为我提供了一种执行直接 sql select/nonquery 的方法?

提前致谢。

Now don't go getting all huffy just yet. This is only for a very specific situation. So rather than asking why I would EVER want to send in-line string SQL through EF, let's just try to stay on the topic of "how".

Do I need to do it the old-fashioned route using regular old ADO.NET or does EF provide me with a way to execute a straight sql select/nonquery?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆躺在深渊里 2024-10-23 06:46:30

您是否研究过Entity SQL

实体框架问答

string city = "London";
using (Entities entities = new Entities()) 
{
  ObjectQuery<Customers> query = entities.CreateQuery<Customers>(
    "SELECT VALUE c FROM Customers AS c WHERE c.Address.City = @city",
    new ObjectParameter("city", city)
  );

  foreach (Customers c in query)
    Console.WriteLine(c.CompanyName);
}

由于 Entity SQL 目前缺乏任何
DML 构造,不可能
问题插入、更新或删除
使用 Entity SQL 和对象的命令
服务

Have you investigated Entity SQL?

Entity Framework Q&A:

string city = "London";
using (Entities entities = new Entities()) 
{
  ObjectQuery<Customers> query = entities.CreateQuery<Customers>(
    "SELECT VALUE c FROM Customers AS c WHERE c.Address.City = @city",
    new ObjectParameter("city", city)
  );

  foreach (Customers c in query)
    Console.WriteLine(c.CompanyName);
}

Since Entity SQL currently lacks any
DML constructs, it is not possible to
issue Insert, Update, or Delete
commands using Entity SQL and Object
Services

听,心雨的声音 2024-10-23 06:46:30

作为 ObjectQuery 的替代方案,如果您需要更多选项来跨越多个数据库表,请使用 context.Database.SqlQuery 和 SqlParameter >。

例子:

var searchId = new Guid("16144A52-A092-4876-9C55-A0AD0109F08A");

var sqlparam = new SqlParameter("sid", searchId);

using (var context = new Entities())
{
    var sql = @"SELECT t1.Name, t2.ChildName
        FROM dbo.Table1 as t1 
        INNER JOIN dbo.Table1 as t2 
            ON t1.Id = t2.ParentId 
        WHERE t1.Id = @sid"

    var result = context.Database.SqlQuery<Combined>(sql, sqlparam);

    foreach(var r in result)
    {
         Console.WriteLine(r.Name + r.ChildName)
    }
}


public class Combined
{
    public string Name { get; set; }
    public string ChildName { get; set; }
}

As an alternative to ObjectQuery if you need more options to cross over multiple database tables, make use of context.Database.SqlQuery<> and SqlParameter.

Example:

var searchId = new Guid("16144A52-A092-4876-9C55-A0AD0109F08A");

var sqlparam = new SqlParameter("sid", searchId);

using (var context = new Entities())
{
    var sql = @"SELECT t1.Name, t2.ChildName
        FROM dbo.Table1 as t1 
        INNER JOIN dbo.Table1 as t2 
            ON t1.Id = t2.ParentId 
        WHERE t1.Id = @sid"

    var result = context.Database.SqlQuery<Combined>(sql, sqlparam);

    foreach(var r in result)
    {
         Console.WriteLine(r.Name + r.ChildName)
    }
}


public class Combined
{
    public string Name { get; set; }
    public string ChildName { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文