是否有可能在EF中使用存储过程返回像传统ADO.net中的原始DataTable/DataSet,而不是返回映射/转换的实体?实体框架:使用存储过程返回原始表结果
1
A
回答
1
EF构建于ADO.NET基础之上,所以无论何时您需要直接访问DbConnection以获取DbContext并直接使用它。例如
using (var db = new MyDbContext())
{
db.Database.Connection.Open();
var con = (SqlConnection)db.Database.Connection;
var cmd = new SqlCommand("exec MyProc", con);
DataTable dt = new DataTable();
using (var rdr = cmd.ExecuteReader())
{
dt.Load(rdr);
}
//. . .
0
是的,这是可能的。
下面我描述了我是如何做到这一点的。存储过程是数据库的一部分。因此,最好将存储过程添加到访问数据库的类并创建数据库模型:dbContext。
public class MyDbContext : DbContext
{
// TODO: add DbSet properties
#region stored procedures
// TODO (1): add a function that calls the stored procedure
// TODO (2): add a function to check if the stored procedure exists
// TODO (3): add a function that creates the stored procedure
// TODO (4): make sure the stored procedure is created when the database is created
#endregion stored procedure
}
TODO(1):过程调用存储过程:
private const string MyStoredProcedureName = ...;
private const string myParamName1 = ...;
private const string myParamName2 = ...;
public void CallMyStoredProcedure(MyParameters parameters)
{
object[] functionParameters = new object[]
{
new SqlParameter(myParamName1, parameters.GetParam1Value(),
new SqlParameter(myParamName2, parameters.GetParam2Value(),
... // if needed add more parameters
};
const string sqlCommand = @"Exec " + MyStoredProcedureName
+ " @" + myParamName1
+ ", @" + myParamName2
... // if needed add more parameters
;
this.Database.ExecutSqlCommand(sqlComman, functionParameters);
}
TODO(2)检查存储过程存在
// returns true if MyStoredProcedure exists
public bool MyStoredProcedureExists()
{
return this.ProcedureExists(MyStoredProcedureName);
}
// returns true if stored procedure with procedureName exists
public bool StoredProcedureExists(string procedureName)
{
object[] functionParameters = new object[]
{
new SqlParameter(@"procedurename", procedureName),
};
string query = @"select [name] from sys.procedures where name= @procedurename";
return this.Database.SqlQuery<string>(query, functionParameters)
.AsEnumerable() // bring to local memory
.Where(item => item == procedureName) // take only the procedures with desired name
.Any(); // true if there is such a procedure
}
TODO(3 )创建存储过程:
public void CreateProcedureUpdateUsageCosts(bool forceRecreate)
{
bool storedProcedureExists = this.MyStoredProcedureExists();
// only create (or update) if not exists or if forceRecreate:
if (!storedProcedureExists || forceRecreate)
{ // create or alter:
Debug.WriteLine("Create stored procedures");
// use a StringBuilder to create the SQL command that will create the
// stored procedure
var x = new StringBuilder();
// ALTER or CREATE?
if (!storedProcedureExists)
{
x.Append(@"CREATE");
}
else
{
x.Append(@"ALTER");
}
// procedure name:
x.Append(@" procedure ");
x.AppendLine(MyStoredProcedureName);
// parameters: (only as an example)
x.AppendLine(@"@ReportPeriod int,");
x.AppendLine(@"@CustomerContractId bigint,");
x.AppendLine(@"@CallType nvarChar(80),");
// etc.
// code
x.AppendLine(@"as");
x.AppendLine(@"begin");
// only as example some of my procedure
x.AppendLine(@"Merge [usagecosts]");
x.AppendLine(@"Using (Select @ReportPeriod as reportperiod,");
x.AppendLine(@" @CustomerContractId as customercontractId,");
...
x.AppendLine(@"end");
// execute the created SQL command
this.Database.ExecuteSqlCommand(x.ToString());
}
}
TODO(4)确保创建
当数据库在MyDbContext创建存储过程:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// TODO: if needed add fluent Api to build model
// create stored procedure
this.CreateProcedureUpdateUsageCosts(true);
}
用法:
using (var dbContext = new MyDbContext(...))
{
MyParameters parms = FillMyParameters();
dbContext.CallMyStoredProcedure(parms);
}
相关问题
- 1. 实体框架存储过程只返回一个结果
- 2. 实体框架存储过程结果
- 3. 实体框架与返回实体的'获取'存储过程
- 4. 实体框架 - 具有多个结果集的存储过程 - 无行返回
- 5. 存储过程返回int值,而不是在实体结果集框架
- 6. 实体框架5存储过程返回int而不是结果集
- 7. 保存SELECT存储过程的结果ASP.NET实体框架
- 8. 返回实体框架结果为JSON
- 9. 实体框架7存储过程结果到模型
- 10. 实体框架4.0 - 函数导入 - 存储过程结果集
- 11. 实体框架存储过程结果映射
- 12. 具有多个结果集的实体框架存储过程?
- 13. 实体框架存储过程结果到域模型
- 14. 实体框架3.5和存储过程结果映射
- 15. 实体框架4 - 映射存储过程返回Guid
- 16. 实体框架7 FromSql存储过程返回值
- 17. 实体框架:如何运行存储过程并返回值?
- 18. SQL存储过程返回值到实体框架
- 19. 实体框架 - 存储过程返回值
- 20. 实体框架4存储过程返回无
- 21. 实体框架存储过程返回类型已更改
- 22. 实体框架:在存储过程后返回空值
- 23. PLSQL存储过程在实体没有返回值框架
- 24. 返回多个实体的结果的存储过程
- 25. 调用存储过程,它不会返回使用实体框架
- 26. 实体框架3.5,将存储过程结果映射到自定义实体
- 27. 实体框架的存储过程映射结果不是唯一的实体
- 28. 将存储过程结果映射到实体框架中的实体5
- 29. 使用实体框架读取存储过程的非键入结果
- 30. 无法读取使用实体框架从存储过程返回的值