How to call Stored Procedures with EntityFramework?

前端 未结 7 1334
青春惊慌失措
青春惊慌失措 2020-12-01 02:03

I have generated an EF4 Model from a MySQL database and I have included both StoredProcedures and Tables.

I know how to make regular instert/update/fetch/delete oper

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 02:29

    This is what I recently did for my Data Visualization Application which has a 2008 SQL Database. In this example I am recieving a list returned from a stored procedure:

    public List GetCumulativeInstrumentLogs(RunLogFilter filter)
        {
            EFDbContext db = new EFDbContext();
            if (filter.SystemFullName == string.Empty)
            {
                filter.SystemFullName = null;
            }
            if (filter.Reconciled == null)
            {
                filter.Reconciled = 1;
            }
            string sql = GetRunLogFilterSQLString("[dbo].[rm_sp_GetCumulativeInstrumentLogs]", filter);
            return db.Database.SqlQuery(sql).ToList();
        }
    

    And then this extension method for some formatting in my case:

    public string GetRunLogFilterSQLString(string procedureName, RunLogFilter filter)
            {
                return string.Format("EXEC {0} {1},{2}, {3}, {4}", procedureName, filter.SystemFullName == null ? "null" : "\'" + filter.SystemFullName + "\'", filter.MinimumDate == null ? "null" : "\'" + filter.MinimumDate.Value + "\'", filter.MaximumDate == null ? "null" : "\'" + filter.MaximumDate.Value + "\'", +filter.Reconciled == null ? "null" : "\'" + filter.Reconciled + "\'");
    
            }
    

提交回复
热议问题