如何在实体framewrok核心中将sql过程作为原始查询执行

问题描述 投票:0回答:1

什么是EF Core2.0中SqlQuery的替代品,我不想创建用于执行该过程的模型

     var result =  _context.Database.SqlQuery<string>("TWS_config_MidOfficeSystems_Validate @mo,@ga,@pcc,@xmo",
                                   parameters: new SqlParameter[]
                                   {   new SqlParameter("@mo",lineData),
                                    new SqlParameter("@ga", gdsAccessId),
                                    new SqlParameter("@pcc", crsPCC),
                                    new SqlParameter("@xmo", bkMidOfficeCode),
                                   }).FirstOrDefaultAsync();
asp.net-core ef-core-2.0
1个回答
0
投票
由于EF Core紧密耦合,您必须使用来自存储过程的属性来创建class

public class ExampleClass { public string Name { get; set; } public string Email { get; set; } }

ApplicationDbContext.cs中将该类添加为DbQuery

public DbQuery<ExampleClass> ExampleClassDbQuery { get; set; }

Dependency Injection插入您的controllerservices

private readonly ApplicationDbContext _db; public ExampleService(ApplicationDbContext applicationDbContext) { _db = applicationDbContext; }

然后像下面这样调用strore过程

_db.ExampleClassDbQuery.FromSql("storeProcedureName", []parameters);

© www.soinside.com 2019 - 2024. All rights reserved.