Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架核心数据库是否先不呈现存储过程的模型? 数据库_C#_Stored Procedures_Entity Framework Core_Asp.net Core Mvc_Database First - Fatal编程技术网

C# 实体框架核心数据库是否先不呈现存储过程的模型? 数据库

C# 实体框架核心数据库是否先不呈现存储过程的模型? 数据库,c#,stored-procedures,entity-framework-core,asp.net-core-mvc,database-first,C#,Stored Procedures,Entity Framework Core,Asp.net Core Mvc,Database First,我有存储过程名GetAllCustomer 我在EntityFramework 6x和Project ASP.NET MVC5中有存储过程 我通过调用db.GetAllCustomer.ToList()来使用 但是在EntityFramework核心中没有呈现DbSet,我需要手动创建它。它不能使用EF 6x 这是我作品的图像: 是否有一种方法可以调用像ef6x这样简单的存储过程???您可以在上下文中使用自定义ExecuteQuery进行任何用途,包括存储过程 public List<

我有存储过程名GetAllCustomer


我在EntityFramework 6x和Project ASP.NET MVC5中有存储过程

我通过调用
db.GetAllCustomer.ToList()来使用

但是在EntityFramework核心中没有呈现DbSet,我需要手动创建它。它不能使用EF 6x

这是我作品的图像


是否有一种方法可以调用像ef6x这样简单的存储过程???您可以在上下文中使用自定义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;
            }
        }
    }
public List ExecuteQuery(字符串查询),其中T:class,new()
{
使用(var命令=Context.Database.GetDbConnection().CreateCommand())
{
command.CommandText=查询;
command.CommandType=CommandType.Text;
Context.Database.OpenConnection();
列出结果;
使用(var reader=command.ExecuteReader())
{
结果=新列表();
var columns=new T().GetType().GetProperties().ToList();
while(reader.Read())
{
var obj=新的T();
对于(变量i=0;ia.Name.ToLower().Equals(Name.ToLower());
if(prop==null)
继续;
var val=reader.IsDBNull(i)?null:reader[i];
属性设置值(对象、值、空);
}
结果:添加(obj);
}
返回结果;
}
}
}
用法:

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")
db.ExecuteQuery(“从您的\u表\u名称中选择字段”)
db.ExecuteQuery(“执行您的SP名称”)
db.ExecuteQuery(“EXEC YOUR_SP_NAME@Id=10”)
为了减少错误,创建查询字符串更容易,速度更快,我使用了其他几种方法,并将存储过程名称放在一个静态类中

例如,我有这样的东西来获取客户列表:

    /// <param name="parameters">The model contains all SP parameters</param>
    public List<customerGetDto> Get(CustomerSpGetParameters parameters = null)
    {
        //StoreProcedures.Customer.Get Is "sp_GetAllCustomers"
        //CreateSqlQueryForSp creates a string with sp name and parameters
        var query = _publicMethods.CreateSqlQueryForSp(StoreProcedures.Request.Get, parameters);
        //For example, query= "Exec sp_GetAllCustomers @active = 1,@level = 3,...."
        return _unitOfWork.ExecuteQuery<RequestGetDto>(query);
    }
///模型包含所有SP参数
公共列表获取(CustomerSpGetParameters=null)
{
//StoreProcedures.Customer.Get是“sp_GetAllCustomers”
//CreateSqlQueryForSp使用sp名称和参数创建字符串
var query=_publicMethods.CreateSqlQueryForSp(StoreProcedures.Request.Get,参数);
//例如,query=“Exec sp_GetAllCustomers@active=1,@level=3,…”
返回unitOfWork.ExecuteQuery(查询);
}

EF Core电动工具可以为您映射存储过程,它不是EF Core的内置功能

示例用户代码:

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);
}
使用(var db=new NorthwindContext())
{
var程序=新的NorthwindContextProcedures(db);
风险值订单=等待程序。客户订单历史记录(“ALFKI”);
foreach(订单中的var订单)
Console.WriteLine($“{order.ProductName}:{order.Total}”);
var outOverallCount=新的OutputParameter();
var客户=等待程序。SP获取TOP ID(10,超过总数);
WriteLine($“Db包含{outOverallCount.Value}个客户。”);
foreach(客户中的var客户)
Console.WriteLine(customer.CustomerId);
}

阅读更多信息:

这正是我需要的。非常感谢。如何使用依赖项注入NorthwindContextProcedures@ErikejDependence注入DbContext,并使用GetProcedures()方法。