How to resolve lambda expression conversion error of type Servicelifetime net core?

Asked

Viewed 484 times

0

I am starting a very simple project with net core 2.2 and confronted myself with the following error:
Cannot convert lambda expression to "Servicelifetime" type because it is not a delegated type

Archive startup.Cs

public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext<Menu>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

inserir a descrição da imagem aqui

Model:

public class Menu
    {
        public int MenuID { get; set; }
        public string NmMenu {get; set;}
        public int MenuIdPai { get; set; }

    }

Context:

public class Ctx: DbContext
    {
        public Ctx(DbContextOptions<Ctx> options)
            : base(options)
        { }

        public DbSet<Menu> Menus { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Menu>().ToTable("Menu");
        }
    }

appsettings:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Ctx;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

1 answer

2


You are trying to add context to your and entity Menu and not for the class that actually inherits the DbContext which in this case is the Ctx. Change your code to

services.AddDbContext<Ctx>(options => 
                           options.UseSqlServer(
                               Configuration.GetConnectionString("DefaultConnection")
                           ));

And I would recommend you to use a real instance of SQL Server instead of localdb, That will save you other headaches later on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.