ASP.NET Core: DbSet to executing raw stored procedure

非 Y 不嫁゛ 提交于 2019-12-25 00:18:00

问题


I am using ASP.NET Core 2.0 and razor pages. The SQL table does not have a primary key and I will not be able to make changes to the SQL table. I am trying to use a stored procedure to retrieve the data from the table and show the resultant data in UI.

Since there is no primary key available, I am getting error as - Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys. I would like to move the code from DBSet to Raw sql as defined in https://www.learnentityframeworkcore.com/raw-sql Below is my existing code :

//Data - myDbContext

 public class MyDbContext : DbContext
    {
        public DbSet<LOB> lobs { get; set; }

        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
        {
        }

        }

// Model  
 public class LOB
    {
        public string Desc { get; set; }
    }

//Index.cshtml.cs

 public class IndexModel : PageModel
    {
        private readonly MyDbContext _dbContext;
        public IndexModel(MyDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        public List<LOB> lOBs { get; set; } = new List<LOB>();

                [BindProperty]
        public string[] SelectedLOBs { get; set; }

        public SelectList LOBOptions { get; set; }


        public async Task OnGetAsync()
        {
            lOBs = await _dbContext.Set<LOB>().FromSql(
                              "EXECUTE sp")
                              .AsNoTracking()
                              .ToListAsync();

            LOBOptions = new SelectList(lOBs, "Desc1");
        }
    }

// Index.cshtml

  <select class="form-control" required multiple id="selLOB" asp-for="SelectedLOBs" asp-items="Model.LOBOptions"></select>

How to fill the dropdown using context.database property ? Thanks


回答1:


For Asp.Net Core and EF Core are different. Asp.Net Core 2.0 is corresponding to Microsoft.AspNetCore.All 2.0 and EF Core 2.1 is correspoinding to Microsoft.EntityFrameworkCore 2.1, you could refer Microsoft.EntityFrameworkCore 2.1 in Microsoft.AspNetCore.All 2.0.

Follow steps below to resolve your issue.

  1. Update package Microsoft.EntityFrameworkCore to V2.2.3 and Microsoft.EntityFrameworkCore.Tools to V2.2.3
  2. Change DbSet to DbQuery

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        public DbQuery<LOB> lobs { get; set; }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }
    }
    
  3. Change your razor code.

    public async Task OnGetAsync()
    {
        lOBs = await _dbContext.Query<LOB>().FromSql(
                          "EXECUTE sp")
                          .AsNoTracking()
                          .ToListAsync();
    
        LOBOptions = new SelectList(lOBs, "Desc", "Desc", "Desc1");
    }
    
  4. Change your view

    <select class="form-control" required multiple id="selLOB" 
        asp-for="SelectedLOBs" asp-items="Model.LOBOptions">
    </select>
    


来源:https://stackoverflow.com/questions/55096502/asp-net-core-dbset-to-executing-raw-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!