Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert lambda expression to type 'ServiceLifetime' because it is not a delegate type on Asp.net core 2.2

Why do I get this error:

Cannot convert lambda expression to type 'ServiceLifetime' because it is not a delegate type [TokenAuthWebApiCore.Server]

on this line of code:

public virtual void SetUpDataBase(IServiceCollection services)
{
    // Add framework services.
        services.AddDbContext<SecurityContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("SecurityConnection"), sqlOptions => sqlOptions.MigrationsAssembly("TokenAuthWebApiCore.Server")));
}

This is how I use it:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton(Configuration);

    services.AddMvc();

    SetUpDataBase(services);

    // services.AddDbContext<SecurityContext>(options =>
    //      options.UseSqlServer(Configuration.GetConnectionString("SecurityConnection"), sqlOptions => sqlOptions.MigrationsAssembly("TokenAuthWebApiCore.Server")));

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

public virtual void SetUpDataBase(IServiceCollection services)
{
    // Add framework services.
        services.AddDbContext<SecurityContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("SecurityConnection"), sqlOptions => sqlOptions.MigrationsAssembly("TokenAuthWebApiCore.Server")));
}

This are all my using statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.IdentityModel.Tokens;
using System.Text;

I am thinking probably this is because the tutorial I am following a tutorial for a different version of .net core and I am using .net core version 2.2. Can you please show me how to fix this? Thank you.

like image 756
Ibanez1408 Avatar asked Apr 15 '19 07:04

Ibanez1408


1 Answers

Not about version compatibility error.You need to implement DbContext in your project. When you make context class, you need to inherit context class from DbContext class.

like image 79
Hassan Muhammad Saad Avatar answered Sep 28 '22 08:09

Hassan Muhammad Saad