Asp.net core ASP.NET Core 3.1:在视图模型中使用存储过程

Asp.net core ASP.NET Core 3.1:在视图模型中使用存储过程,asp.net-core,stored-procedures,viewmodel,viewbag,Asp.net Core,Stored Procedures,Viewmodel,Viewbag,下面的代码用于运行存储过程并将值(行)返回到varReturnedRowsFromSP: var recordValue = new SqlParameter("@thisRecordValue", RecordValueFromInput); var ReturnedRowsFromSP = _context.ReturnedRowsTableExistsInDB.FromSqlRaw("exec sp_MySpReturnsManyRows @thisReco

下面的代码用于运行存储过程并将值(行)返回到var
ReturnedRowsFromSP

var recordValue = new SqlParameter("@thisRecordValue", RecordValueFromInput);

var ReturnedRowsFromSP = _context.ReturnedRowsTableExistsInDB.FromSqlRaw("exec sp_MySpReturnsManyRows @thisRecordValue", recordValue)
                                 .AsNoTracking().ToList();
我这里的问题是,我需要创建一个类,进行迁移,并将表(
ReturnedRowsTableExistsInDB
)添加到数据库中,以便使用存储过程,并且该类需要与存储过程中返回的列完全相同

有没有办法将存储过程与表(未在数据库中创建)或视图模型一起使用,甚至将结果(存储过程的结果)传递给
ViewBag
ViewData


非常感谢

您可以在
DbContext
类中将
viewmodel
定义为DbSet,然后忽略以创建表。试试看

public class DbContext : DbContext
{
    public DbSet<StoredProcedureViewModel> spVM { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Ignore<StoredProcedureViewModel>();
    }
}
公共类DbContext:DbContext
{
公共DbSet spVM{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder)
{
builder.Ignore();
}
}

注意:在
OnModelCreating
中,您可以忽略
class
DbSet
来创建数据库表。

您可以在
DbContext
类中将
viewmodel
定义为DbSet,然后忽略来创建表。试试看

public class DbContext : DbContext
{
    public DbSet<StoredProcedureViewModel> spVM { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Ignore<StoredProcedureViewModel>();
    }
}
公共类DbContext:DbContext
{
公共DbSet spVM{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder)
{
builder.Ignore();
}
}

注意:在
OnModelCreating
中,您可以忽略
class
DbSet
来创建数据库表。

您可以参考以下步骤使用EF-core查询存储过程(无需为存储过程结果创建表)

  • 在SQL server数据库中创建存储过程:

     CREATE PROCEDURE sp_GetBookAuthors @bookname nvarchar(250)
     AS
    
     select b.Id, b.BookName, a.AuthorName, b.ISBN from Authors as a 
              join BookAuthors as ba 
              on a.Id = ba.AuthorId 
              join Books as b 
              on ba.BookId = b.Id
              where b.BookName = @bookname
    
  • 根据查询结果创建以下ViewModel:

     public class BookAuthorViewModel
     {
         [Key]
         public int Id { get; set; }
         public string BookName { get; set; }
         public string AuthorName { get; set; }
         public string ISBN { get; set; }
     }
    
  • 在DB上下文OnModelCreating方法中注册存储过程:

     public class WebApplication2Context : IdentityDbContext<WebApplication2User>
     { 
         public DbSet<Book> Books { get; set; }
         public DbSet<Author> Authors { get; set; }
         public DbSet<BookAuthor> BookAuthors { get; set; } 
    
         public WebApplication2Context(DbContextOptions<WebApplication2Context> options)
             : base(options)
         {
         }
    
         protected override void OnModelCreating(ModelBuilder builder)
         {
             base.OnModelCreating(builder);
             builder.Entity<BookAuthor>().HasKey(sc => new { sc.AuthorId, sc.BookId });
    
             #pragma warning disable CS0618 // Type or member is obsolete
             builder.Ignore<BookAuthorViewModel>(); //ignore create the table for the stored procedure
             builder.Query<BookAuthorViewModel>();  //register stored procedure.
             #pragma warning restore CS0618 // Type or member is obsolete
         }
    
         //create a method for the stored procedure.
         public List<BookAuthorViewModel> GetBookAuthors(string sqlQuery)
         {
             // Initialization.  
             List<BookAuthorViewModel> lst = new List<BookAuthorViewModel>();
    
             try
             { 
                 #pragma warning disable CS0618 // Type or member is obsolete
                 lst = this.Query<BookAuthorViewModel>().FromSqlRaw<BookAuthorViewModel>(sqlQuery).ToList();
                 #pragma warning restore CS0618 // Type or member is obsolete
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             // Info.  
             return lst;
         }
     }
    
    公共类WebApplication2Context:IdentityDbContext
    { 
    公共数据库集书籍{get;set;}
    
    public DbSet

    您可以参考以下步骤使用EF core查询存储过程(无需为存储过程结果创建表)

  • 在SQL server数据库中创建存储过程:

     CREATE PROCEDURE sp_GetBookAuthors @bookname nvarchar(250)
     AS
    
     select b.Id, b.BookName, a.AuthorName, b.ISBN from Authors as a 
              join BookAuthors as ba 
              on a.Id = ba.AuthorId 
              join Books as b 
              on ba.BookId = b.Id
              where b.BookName = @bookname
    
  • 根据查询结果创建以下ViewModel:

     public class BookAuthorViewModel
     {
         [Key]
         public int Id { get; set; }
         public string BookName { get; set; }
         public string AuthorName { get; set; }
         public string ISBN { get; set; }
     }
    
  • 在DB上下文OnModelCreating方法中注册存储过程:

     public class WebApplication2Context : IdentityDbContext<WebApplication2User>
     { 
         public DbSet<Book> Books { get; set; }
         public DbSet<Author> Authors { get; set; }
         public DbSet<BookAuthor> BookAuthors { get; set; } 
    
         public WebApplication2Context(DbContextOptions<WebApplication2Context> options)
             : base(options)
         {
         }
    
         protected override void OnModelCreating(ModelBuilder builder)
         {
             base.OnModelCreating(builder);
             builder.Entity<BookAuthor>().HasKey(sc => new { sc.AuthorId, sc.BookId });
    
             #pragma warning disable CS0618 // Type or member is obsolete
             builder.Ignore<BookAuthorViewModel>(); //ignore create the table for the stored procedure
             builder.Query<BookAuthorViewModel>();  //register stored procedure.
             #pragma warning restore CS0618 // Type or member is obsolete
         }
    
         //create a method for the stored procedure.
         public List<BookAuthorViewModel> GetBookAuthors(string sqlQuery)
         {
             // Initialization.  
             List<BookAuthorViewModel> lst = new List<BookAuthorViewModel>();
    
             try
             { 
                 #pragma warning disable CS0618 // Type or member is obsolete
                 lst = this.Query<BookAuthorViewModel>().FromSqlRaw<BookAuthorViewModel>(sqlQuery).ToList();
                 #pragma warning restore CS0618 // Type or member is obsolete
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             // Info.  
             return lst;
         }
     }
    
    公共类WebApplication2Context:IdentityDbContext
    { 
    公共数据库集书籍{get;set;}
    
    public DbSet

    SqlParameter TitleParam=new SqlParameter(“@Title”,Title);wait_context.Database.ExecuteSqlCommandAsync(“Sp_insert@Title”,TitleParam);如果存储过程要将数据插入到表中,则此方法可以正常工作。但是,如果不使用ApplicationBContextSqlParameter TitleParam=new SqlParameter(“@Title”,Title)”中定义的表/类,则无法为Select计算出它;等待_context.Database.ExecuteSqlCommandAsync(“Sp_insert@Title,TitleParam);如果存储过程要向表中插入数据,则此操作很好。但是,如果不使用ApplicationDBContext中定义的表/类,则无法为Select计算出它。非常感谢您的回答。实际上,它部分工作。是的,它不创建表,我可以在代码中使用它。但是,当我在stor中运行它时ed过程行它给出了此错误(InvalidOperationException:无法为“StoredProcedureViewModel”创建数据库集,因为上下文的模型中不包含此类型)…虽然与您建议的相同,但我在上下文中使用了它。非常感谢您的答复..实际上它起了部分作用..是的,它不创建表,我可以在代码中使用它..但是当我在存储过程行中运行它时,会出现此错误(InvalidOperationException:无法为“StoredProcedureViewModel”创建数据库集,因为该类型未包含在上下文的模型中)…虽然与您建议的相同,但我在上下文中有它。非常感谢您非常感谢