5

I specify the context class I created in the entity project on the Startup.cs file and the connectionString data I created for connectionString. But why am I getting this error?

ERROR message: Severity Code Description Project File Line Suppression State Error CS0311 The type 'Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext' cannot be used as type parameter 'TContext' in the generic type or method 'EntityFrameworkServiceCollectionExtensions.AddDbContext(IServiceCollection, Action, ServiceLifetime, ServiceLifetime)'. There is no implicit reference conversion from 'Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext' to 'Microsoft.EntityFrameworkCore.DbContext'. EntityFramework2 C:\Users\xsamu\source\repos\EntityFramework2\EntityFramework2\Startup.cs 29 Active

Startup class:

namespace EntityFramework2
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddDbContext<UserContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

Entity configuration:

namespace EntityFramework2
{
    public class EntityConfiguration : IEntityTypeConfiguration<User>
    {
        public void Configure(EntityTypeBuilder<User> builder)
        {
            builder.HasOne<Department>(navigationExpression: s => s.Name)
                .WithOne(sa => sa.User)
                .HasForeignKey<Department>(sa => sa.DepartmentId);

            builder.HasOne<Title>(navigationExpression: s => s.TitleCode)
               .WithOne(sa => sa.User)
               .HasForeignKey<Title>(sa => sa.TitleId);

            builder.HasOne<Position>(navigationExpression: s => s.PositionCode)
               .WithOne(sa => sa.User)
               .HasForeignKey<Position>(sa => sa.PositionId);
        }
    }
}
satma0745
  • 667
  • 1
  • 7
  • 26
el ahmadi
  • 51
  • 1
  • 1
  • 3

4 Answers4

8

There is no implicit reference conversion from 'Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext' to 'Microsoft.EntityFrameworkCore.DbContext'.

The message tells you, that your UserContext class does not inherit from DbContext, which is mandatory.

It should look something like this:

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

    public DbSet<Blog> Blogs { get; set; }
}

For further information, see the EF Core Tutorial and Configuring a DbContext.

lauxjpn
  • 4,749
  • 1
  • 20
  • 40
  • What about if I am inheriting from IdentityDbContext ? – Subliminal Hash Dec 23 '22 at 11:06
  • @SubliminalHash That is fine, since it in turn inherits from `DbContext`. From [IdentityDbContext Class](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.entityframeworkcore.identitydbcontext?view=aspnetcore-7.0): Inheritance `DbContext` → `IdentityUserContext` → `IdentityDbContext,IdentityUserRole,IdentityUserLogin,IdentityRoleClaim,IdentityUserToken>` → `IdentityDbContext` → `IdentityDbContext` – lauxjpn Dec 24 '22 at 02:54
  • it didn't like me using IdentityContext. Instead I left it at DbContext it worked fine. Thanks for the comment. – Subliminal Hash Dec 24 '22 at 05:41
2

Does Your UserContext inherit DbContext class?

satma0745
  • 667
  • 1
  • 7
  • 26
1

You get this message when you have a call and its interface but you forgot to let the class inherit the interface.

class UserContext : MyInterface

0

Just make Your Context UserContext inherited from DbContext Class like the following Code and you are good to go:

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

    public DbSet<User> Users{ get; set; }
}
Abdessamad Jadid
  • 381
  • 1
  • 4
  • 16