How to resolve ServiceLifetime net core lambda expression conversion error?
I am starting a very simple project with net core 2.2 and faced with the following error:
cannot convert lambda expression to type "ServiceLifetime" because it is not a delegate type
Startup file.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);
}
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"
}
}
}
0
1 answers
You are trying to add the context to your and entity Menu
and not to the class that actually inherits the DbContext
which in the case is the Ctx
. Change your code to
services.AddDbContext<Ctx>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")
));
And now I would recommend you to use a real instance of SQL Server instead of localdb
, this will save you other headaches later.
2
Author: Leandro Angelo, 2019-06-24 15:33:04