DbContext.set() cannot create a DbSet for entity because this type is not included in the model for the context

asp.net-core entity-framework-6 entity-framework-core

Question

I am using EF Core. I am using DbContext.Set() method but it is giving me the error - "Cannot create a DbSet for 'MediaDate' because this type is not included in the model for the context.'"

Below is my code:

var context = new GoldentaurusContext();
DbSet<MediaDate> set = context.Set<MediaDate>();
mediaDateList = set.FromSql("[dbo].[sp_GetMediaDate]")
                .Select(x => new SelectListItem { Text = x.DateText, Value = x.DateValue })
                .ToList();

The MediaDate class:

public class MediaDate
{
    public string DateText { get; set; }
    public string DateValue { get; set; }
}

enter image description here

Why it is requiring me to add the MediaDate class to the DbContext class?

Please help what I am doing wrong?

1
5
5/10/2018 4:05:29 PM

Popular Answer

Your DB Context class should be like below.

 public partial class DatabaseContext : DbContext
   {

    public DatabaseContext (string ConnectionString) : base(new DbContextOptionsBuilder().UseSqlServer(ConnectionString).Options)
    {

    }  
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Query<MediaData>();            
    }

Add your models in the DatabaseContext using model builder. This is how I have resolved this isssue

2
8/16/2019 7:28:39 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow