Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

The entity type 'AllVM' requires a primary key to be defined for VM with EF Core

$
0
0

Hi

I'm trying to return multiple different non related models in one view as aggregate interface page (each one in html tab) with Entity Framework Core (Core 2.1).

Holidays model:

    public class Holidays
    {
        public int Id { get; set; }

        public string Name{ get; set; }
    }

Actions model:

    public class Actions
    {
        public int Id { get; set; }

        public string Name{ get; set; }
    }

AllVM model:

    public class AllVM
    {
        public IList<Holidays> Holidays { get; set; }
        public IList<Actions> Actions{ get; set; }
    }

ApplicationDBContext:

public virtual DbSet<Actions> Actions { get; set; }
public virtual DbSet<Holidays> Holidays { get; set; }
public virtual DbSet<AllVM> AllVM { get; set; }

Controller:

    public class MyController : Controller
    {
        private readonly ApplicationDbContext _context;

        public MyController(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<IActionResult> AllIndex()
        {
            var all = _context.AllVM.Include(s => s.Holidays).Include(s => s.Actions).AsQueryable();

            return View(await all.ToListAsync());
        }

But this scenario gives me the error:

InvalidOperationException: The entity type 'AllVM' requires a primary key to be defined.

I don't want to define a primary key for the ViewModel, I want only to return 2 or more non-related models in AllIndex view each one of them in a tab.

How please?


Viewing all articles
Browse latest Browse all 9386

Trending Articles