Reputation: 59
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);
}
}
}
Upvotes: 5
Views: 30216
Reputation: 73
This problem happens because of the DbContext inheritance, I had the same problem while I was inheriting from IdentityDbcontext because it was inheriting and old package.
I solved adding this package: Microsoft.AspNetCore.Identity.EntityFrameworkCore
FYI: IdentityDbcontext also inherits from DbContext.
Upvotes: 0
Reputation: 1
I faced similar issues in configuartion class. Earlier, the code was placed as DbMigrationsConfiguration<UserContext>
. However reviewing the error below, the code has been updated and the error gone.
Error CS0311 The type 'EntityFramework2.Data.UserContext' cannot be used as type parameter 'TContext' in the generic type or method 'DbMigrationsConfiguration'. There is no implicit reference conversion from 'EntityFramework2.Data.UserContext' to 'System.Data.Entity.DbContext'.
Updated Code:
internal sealed class Configuration : DbMigrationsConfiguration<DbContext>
Upvotes: -2
Reputation: 792
Entity Framework requires You to inherit Your UserContext
from the DbContext
EFCore's base class.
Please double-check if you really did this (judging by the error message, you forgot about it).
Upvotes: 3
Reputation: 195
You get this message when you have a call and its interface but you forgot to let the class implement the interface.
class UserContext : MyInterface
Upvotes: 1
Reputation: 21
check your nuget package you install Microsoft.AspNet.Identity.EntityFrameworkCore instead of Microsoft.AspNetCore.Identity.EntityFrameworkCore;
AspNet instead of AspNetCore
that should do it
Upvotes: 1
Reputation: 401
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; }
}
Upvotes: 0
Reputation: 5254
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.
Upvotes: 9