Stack Overflow Asked by el ahmadi on November 7, 2021
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:UsersxsamusourcereposEntityFramework2EntityFramework2Startup.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);
}
}
}
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; }
}
Answered by Abdessamad Jadid on November 7, 2021
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.
Answered by lauxjpn on November 7, 2021
Does Your UserContext
inherit DbContext
class?
Answered by catman0745 on November 7, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP