【问题标题】:Execute stored procedure using entity framework [duplicate]使用实体框架执行存储过程[重复]
【发布时间】:2016-12-28 00:39:25
【问题描述】:

是否可以使用 EF 执行存储过程,使用内连接和左外连接从两个或多个表中选择数据库中的记录。

我的观点是避免在 EF 或 LINQ 中进行连接的方法,我对此有很多问题。

因此,如果我创建该过程,我可以使用用户输入的参数调用它吗,可以将结果分配给 .ToList() 方法,然后将结果添加到 asp:repeater .DataSource

我知道这可能是一个奇怪的问题,但我想这样做有很多原因 首先,使用EF,因为我感觉更舒服。 第二,摆脱在 EF 中使用连接。 第三,我在某处读到使用存储过程将提高查询性能,当用于频繁调用查询时。

如果有人可以通过示例帮助我回答这些问题,我将不胜感激。

【问题讨论】:

  • sql-server 和 mysql? EF也是先编码吗?

标签: c# mysql sql-server entity-framework linq


【解决方案1】:

您可以从您的实体框架数据上下文中调用SqlQuery

context.Database.SqlQuery<YourType>("exec usp_StoredProcedure").ToList()

您需要一个类来映射查询结果,例如:

public class YourType
{
   public string Property1 { get; set; }
   public string Property2 { get; set; }
}

您还可以为查询指定参数,如下所示:

SqlParameter parameter1 = new SqlParameter("@Parameter1", "Value");
context.Database.SqlQuery<YourType>("exec usp_StoredProcedure @Parameter1", parameter1).ToList()

【讨论】:

  • 是的,我知道@darren,但在我的情况下调用这个查询非常复杂,并且会在我的应用程序中经常使用,所以我更喜欢使用 SP 调用。我的问题是如何为结果调用.ToList() 方法。
  • @FaresAyyad - 我已经给出了执行 SP 的解决方案。似乎是什么问题?
  • @DarrrenDavies 获取查询结果的类是什么?
  • @FaresAyyad - 您必须创建一个代表您从存储过程返回的列。我为你提供了一个虚拟的,叫做YourType
  • 你知道当你忘记了做这种简单事情的语法并且不得不谷歌它时,你就会遇到你的老朋友达伦...... :)
【解决方案2】:

我们将看到如何在Entity Framework中执行存储过程,在MVC中我们将看到如何添加EF。

在数据库中执行以下脚本创建存储过程。

CREATE PROCEDURE FETCHEMPLOYEES AS
BEGIN
    SELECT * FROM EMPTABLE
END

CREATE PROCEDURE FETCHEMPLOYEE(@ID INT) AS
BEGIN
  SELECT * FROM EMPTABLE WHERE ID = @ID
END



public class EmpModel
{
    EmployeeEntities empdb = new EmployeeEntities();

    public List<EMPTABLE> GetEmployees()
    {
       return empdb.FETCHEMPLOYEES().ToList();  
    }

    public EMPTABLE GetEmployee(int? id)
    {
        return empdb.FETCHEMPLOYEE(id).ToList().Single();
    }
}

public class EmployeeController : Controller
{
    Models.EmpModel mod = new Models.EmpModel();

    public ActionResult Index()
    {
        List<EMPTABLE> result = mod.GetEmployees();
        return View(result);
    }

    public ActionResult Details(int id)
    {
        EMPTABLE result = mod.GetEmployee(id);
        return View(result);
    }
}

更多详细步骤,请参考以下链接: http://dotnetvisio.blogspot.in/2014/01/execute-stored-procedure-using-entity.html

【讨论】:

    【解决方案3】:

    你可以使用ExecuteFunctionObjectContext类:

     public virtual ObjectResult<CustomClassForStoreReturnedData> NameOfStoredProcedure(Nullable<int> tableID)
        {
            var IDParameter = tableID.HasValue ?
                new ObjectParameter("tableID", tableID) :
                new ObjectParameter("tableID", typeof(int));
    
            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<CustomClassForStoreReturnedData>("NameOfStoredProcedureInSQLDatabase", IDParameter);
        }
    

    【讨论】:

    • @LeresAldtai 我没有看到 OP 对它的方法说任何话
    【解决方案4】:

    您正在使用带有 MySQL 的实体框架:

    在这个例子中,我的存储过程需要一个参数,名为 myParam 键入 BIGINT

    var myParam = new SqlParameter("@myParam", (MySqlDbType.Int64)).Value = 100;
    
    var res = _context.Database.SqlQuery<MyEntity>($"call MyProcedureName({pyParam})");
    

    请注意,我使用C# String Interpolation 来构建我的查询字符串。

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多