Get IEnumerable<SelectListItem> from dbContext

Muflix

I have following database class

namespace Project.Web.Repository
{
    public class Database : DbContext
    {

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

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
        }

        public virtual DbSet<SelectListItem> SelectListItem {get; set;}

        public IEnumerable<SelectListItem> Categories()
        {
            var query = "SELECT Id as [Value], Name as [Text] FROM enum.Category";
            IEnumerable<SelectListItem> items = SelectListItem.FromSql(query).ToList<SelectListItem>();
            return items;
        }

    }
}

But after call

_database.BrandCategories();

I am getting

Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code: 'The entity type 'SelectListGroup' requires a primary key to be defined.'

What is the preferred way of getting select lists from database via EF Core ? Do i need to create POCO class for each table and after getting data convert the POCO class into SelectListItem collection ?

Chris Pratt

You can only map to entity types via FromSql, i.e. types that belong to your context. However, once there, you can use LINQ's Select to map to something like SelectListItem:

var items = categories.Select(x => new SelectListItem { Value  x.Id, Title = x.Name });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert List to IEnumerable<SelectListItem>

From Dev

Get from IQueryable to IEnumerable

From Dev

How to add item to IEnumerable SelectListItem

From Dev

get value from my SelectListItem so it can get into the database

From Dev

Get IEnumerable from LINQ select

From Dev

Convert IDictionary<Guid,string> to the IEnumerable<SelectListItem>

From Dev

Why is there an error, "There is no ViewData item of type 'IEnumerable<SelectListItem>'..."

From Dev

Exception "There is no ViewData item of type 'IEnumerable<SelectListItem>" MVC

From Dev

Convert IDictionary<Guid,string> to the IEnumerable<SelectListItem>

From Dev

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

From Dev

Cast from List to SelectListItem

From Dev

C#: Get distinct T from IEnumerable<IEnumerable<T>>

From Dev

Get data from IEnumerable using IEnumerable<string> as a parameter

From Dev

How to get all DbSet from DbContext

From Dev

Get DbContext from Entity in Entity Framework

From Dev

Get the database type from EntityFramework's `DbContext`

From Dev

Get value from IEnumerable foreach loop

From Dev

How to get's the model metadata from an IEnumerable?

From Dev

How to get's the model metadata from an IEnumerable?

From Dev

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key "key"

From Dev

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Register.CountryId'

From Dev

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key country

From Dev

ASP.NET MVC - Dropdown - There is no ViewData item of type 'IEnumerable<SelectListItem>'

From Dev

Html.DropDownList Selected Value Not Working (Using Constructor with IEnumerable<SelectListItem>)

From Dev

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'CategoryID

From Dev

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'CicekListesi'

From Dev

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'PestType'

From Dev

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Credit'

From Dev

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'CategoryID

Related Related

  1. 1

    Convert List to IEnumerable<SelectListItem>

  2. 2

    Get from IQueryable to IEnumerable

  3. 3

    How to add item to IEnumerable SelectListItem

  4. 4

    get value from my SelectListItem so it can get into the database

  5. 5

    Get IEnumerable from LINQ select

  6. 6

    Convert IDictionary<Guid,string> to the IEnumerable<SelectListItem>

  7. 7

    Why is there an error, "There is no ViewData item of type 'IEnumerable<SelectListItem>'..."

  8. 8

    Exception "There is no ViewData item of type 'IEnumerable<SelectListItem>" MVC

  9. 9

    Convert IDictionary<Guid,string> to the IEnumerable<SelectListItem>

  10. 10

    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

  11. 11

    Cast from List to SelectListItem

  12. 12

    C#: Get distinct T from IEnumerable<IEnumerable<T>>

  13. 13

    Get data from IEnumerable using IEnumerable<string> as a parameter

  14. 14

    How to get all DbSet from DbContext

  15. 15

    Get DbContext from Entity in Entity Framework

  16. 16

    Get the database type from EntityFramework's `DbContext`

  17. 17

    Get value from IEnumerable foreach loop

  18. 18

    How to get's the model metadata from an IEnumerable?

  19. 19

    How to get's the model metadata from an IEnumerable?

  20. 20

    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key "key"

  21. 21

    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Register.CountryId'

  22. 22

    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key country

  23. 23

    ASP.NET MVC - Dropdown - There is no ViewData item of type 'IEnumerable<SelectListItem>'

  24. 24

    Html.DropDownList Selected Value Not Working (Using Constructor with IEnumerable<SelectListItem>)

  25. 25

    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'CategoryID

  26. 26

    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'CicekListesi'

  27. 27

    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'PestType'

  28. 28

    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Credit'

  29. 29

    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'CategoryID

HotTag

Archive