I have 3 projects in my Solution like below:
So in model project i defined my tables ,relations, queries and their classes then added that on ProjectA and ProjectB.
on ProjectB (.net core) in startup.cs by services.AddDbContext tries to add dbcontext (ModelProject) but get this error:
Cannot convert lambda expression to type 'ServiceLifetime' because it is not a delegate type
Is this error for using EF on Core Project? how can i solve it?
ProjectB (Core):
services .AddEntityFrameworkSqlServer() .AddDbContext<Models.DataContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("DataContext")); });
and dbcontext (EntityFramework Standard):
namespace ModelProject
{
public class DataContext : DbContext
{
public DataContext() : base("DataContext")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, Migrations.Configuration>());
(this as IObjectContextAdapter).ObjectContext.ContextOptions.UseCSharpNullComparisonBehavior = true;
((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 300;
}
}
}
It needs to be:
.AddDbContext<Models.DataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DataContext")));
In other words, without the curly brackets. You could alternatively just return the options
instance, but that's all just unnecessary code-bloat. The exception is a red herring. Because your lambda currently is essentially an action (no return) rather than a func, it's not matching the right param for AddDbContext
.