Add Model Project (EntityFramwork) to Asp.net Core 3 Project

asp.net asp.net-core blazor entity-framework entity-framework-core

Question

I have 3 projects in my Solution like below:

  1. ModelProject (EntityFramework)
  2. ProjectA (ASP.NET MVC)
  3. ProjectB (ASP.NET CORE 3 with Blazor)

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;
         }
     }
}
1
0
10/2/2019 10:37:24 AM

Popular Answer

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.

0
10/2/2019 12:50:37 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