[英]Execute stored procedure and return List<T> in Entity Framework Core
New to .NET Core. .NET Core 的新手。 My goal is to return list of employees from my database (as a list or dbset):我的目标是从我的数据库中返回员工列表(作为列表或 dbset):
public class MyDataContext : DbContext
{
public MyDataContext(DbContextOptions<MyDataContext> options)
: base(options)
{
}
public List<Employee> GetEmployeeList(int EmpId, int DeptId)
{
// here I want to call my stored procedure, pass two parameters and execute it
// At this point I am getting error of null argument
using (var context = new MyDataContext(options))
using (var command = new MYDataContext.Database.GetDbConnection().CreateCommand())
{
command.CommandText = "myStoredProcedureName";
command.CommandType = CommandType.StoredProcedure;
context.Database.OpenConnection();
using (var result = command.ExecuteReader())
{
}
}
}
}
in Startup.cs
I have included在Startup.cs
我已经包括
services.AddDbContext<MyDataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
And in appsettings.json
I have my connection string在appsettings.json
我有我的连接字符串
{
"ConnectionStrings": {
"DefaultConnection": "Server=ser11; Database=myDb; User Id=user; Password=123; Trusted_Connection=False; MultipleActiveResultSets=true"
},
"Logging":
{
"LogLevel":
{
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
At the code line在代码行
using (var context = new MyDataContext(options))
I get an error我收到一个错误
There is no argument given that correspond to the required formal parameter 'options'没有给出对应于所需形式参数“选项”的参数
Any idea what I should pass?知道我应该通过什么吗?
This is a method defined on a MyDataContext
, so you don't need to create a new one.这是在MyDataContext
上定义的一种方法,因此您无需创建新方法。
Try this:尝试这个:
public List<Employee> GetEmployeeList(int EmpId, int DeptId)
{
using (var command = Database.GetDbConnection().CreateCommand())
{
command.CommandText = "myStoredProcedureName";
command.CommandType = CommandType.StoredProcedure;
Database.OpenConnection();
using (var result = command.ExecuteReader())
{
}
}
}
Database
is a property of the current object ie this
. Database
是当前对象的一个属性,即this
。
public List<Employee> GetEmployeeList(int EmpId, int DeptId)
{
List<Employee> result = new List<Employee>();
using (var context = new MyDataContext(options))
using (var command = new MYDataContext.Database.GetDbConnection().CreateCommand())
{
command.CommandText = "myStoredProcedureName";
command.CommandType = CommandType.StoredProcedure;
context.Database.OpenConnection();
using (var reader = command.ExecuteReader())
{
if (reader.NextResult())
{
result = new List<Employee>();
while (reader.Read())
{
result.Add(Populators.PopulateEmployee(reader));
}
}
}
}
return result;
}
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;
public class Employee
{
// Column must match the column name returned by the stored procedure
[Column("FirstName")]
public string FirstName { get; set; }
[Column("LastName")]
public string LastName{ get; set; }
}
public partial class MyDataContext: DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>(entity =>
{
entity.HasNoKey();
});
}
}
public async Task<List<Employee>> GetEmployeeList(int EmpId, int DeptId)
{
return await Context.Set<Employee>()
.FromSqlRaw($"EXECUTE dbo.myStoredProcedureName {EmpId} {DeptId}")
.ToListAsync();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.