[英]Entity Framework Core Database First not render Model of Stored Procedure?
I have Stored Procedure name GetAllCustomer我有存储过程名称GetAllCustomer
I have Stored Procedure in the EntityFramework 6x and in Project ASP.NET MVC5.我在 EntityFramework 6x 和项目 ASP.NET MVC5 中有存储过程。
I'm using by calling db.GetAllCustomer.ToList();
我通过调用db.GetAllCustomer.ToList();
But in the EntityFramework Core it's not rendering DbSet and I need to create it manually.但在EntityFramework Core中,它没有呈现DbSet ,我需要手动创建它。 It can't use EF 6x.它不能使用 EF 6x。
Here is the image of my works :这是我的作品的图片:
EF Core Power Tools can map stored procedures for you, it is not a built in feature of EF Core. EF Core Power Tools 可以为您提供 map 存储过程,它不是 EF Core 的内置功能。
Sample user code:示例用户代码:
using (var db = new NorthwindContext())
{
var procedures = new NorthwindContextProcedures(db);
var orders = await procedures.CustOrderHist("ALFKI");
foreach (var order in orders)
Console.WriteLine($"{order.ProductName}: {order.Total}");
var outOverallCount = new OutputParameter<int?>();
var customers = await procedures.SP_GET_TOP_IDS(10, outOverallCount);
Console.WriteLine($"Db contains {outOverallCount.Value} Customers.");
foreach (var customer in customers)
Console.WriteLine(customer.CustomerId);
}
Read more here: https://github.com/ErikEJ/EFCorePowerTools/wiki/Reverse-Engineering#sql-server-stored-procedures在此处阅读更多信息: https://github.com/ErikEJ/EFCorePowerTools/wiki/Reverse-Engineering#sql-server-stored-procedures
You can use custom ExecuteQuery
in your context for any use including stored procedure.您可以在上下文中将自定义ExecuteQuery
用于任何用途,包括存储过程。
public List<T> ExecuteQuery<T>(string query) where T : class, new()
{
using (var command = Context.Database.GetDbConnection().CreateCommand())
{
command.CommandText = query;
command.CommandType = CommandType.Text;
Context.Database.OpenConnection();
List<T> result;
using (var reader = command.ExecuteReader())
{
result = new List<T>();
var columns = new T().GetType().GetProperties().ToList();
while (reader.Read())
{
var obj = new T();
for (var i = 0; i < reader.FieldCount; i++)
{
var name = reader.GetName(i);
var prop = columns.FirstOrDefault(a => a.Name.ToLower().Equals(name.ToLower()));
if (prop == null)
continue;
var val = reader.IsDBNull(i) ? null : reader[i];
prop.SetValue(obj, val, null);
}
result.Add(obj);
}
return result;
}
}
}
Usage:用法:
db.ExecuteQuery<YOUR_MODEL_DEPEND_ON_RETURN_RESULT>("SELECT FIELDS FROM YOUR_TABLE_NAME")
db.ExecuteQuery<YOUR_MODEL_DEPEND_ON_RETURN_RESULT>("EXEC YOUR_SP_NAME")
db.ExecuteQuery<YOUR_MODEL_DEPEND_ON_RETURN_RESULT>("EXEC YOUR_SP_NAME @Id = 10")
To reduce errors, create query string easier and do faster, I use several other methods, and I put stored procedures names in a static class.为了减少错误,更轻松地创建查询字符串并更快地创建查询字符串,我使用了其他几种方法,并将存储过程名称放在 static class 中。
For example, I have something like this to get customer list:例如,我有这样的东西来获取客户列表:
/// <param name="parameters">The model contains all SP parameters</param>
public List<customerGetDto> Get(CustomerSpGetParameters parameters = null)
{
//StoredProcedures.Customer.Get Is "sp_GetAllCustomers"
//CreateSqlQueryForSp creates a string with stored procedure name and parameters
var query = _publicMethods.CreateSqlQueryForSp(StoredProcedures.Request.Get, parameters);
//For example, query= "Exec sp_GetAllCustomers @active = 1,@level = 3,...."
return _unitOfWork.ExecuteQuery<RequestGetDto>(query);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.