简体   繁体   中英

Issue with Entity Framework, the table or view does not have a primary key

I have a small crud app in ASP.NET MVC using Entity Framework, everything works fine, only that I was asked to use a view to add to the crud. In any case this view does not have a primary key and when I want to add the view it jumps to me that error. Is there any way I can fix this without modifying the view?

I attach the example view:

create view view_Prueba 
as
    select nombre, garantia, region, sucursal
    from region, prueba
    where idprue = idprue
  1. Create view in your db using your select query Or using EF
context.Database.ExecuteSqlRaw(
@"Create view view_Prueba as
select nombre, garantia, region, sucursal
from region, prueba
where idprue=idprue");
  1. Create the View_Prueba class that contains nombre, garantia, region, sucursal properties { get; private set; }

  2. Add this view to your ef dbcontext

public virtual DbQuery<View_Prueba> View_Pruebas { get; set; }

.....


  modelBuilder.Entity<View_Prueba>(e =>
            {
                e.ToView("View_Prueba");
                e.HasNoKey();
            }); 

if you use ef core version less then core 5 try this code


public virtual DbQuery<View_Prueba> View_Pruebas { get; set; }

.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM