gpt4 book ai didi

c# - CS0311 C# 该类型不能用作泛型类型或方法中的类型参数 'TContext'。 Entity Framework 核心

转载 作者:行者123 更新时间:2023-12-04 14:44:51 25 4
gpt4 key购买 nike

我指定了我在 Startup.cs 文件的实体项目中创建的上下文类和我为 connectionString 创建的 connectionString 数据。但是为什么我会收到这个错误?

ERROR message:Severity Code Description Project File Line Suppression StateError CS0311 The type'Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext'cannot be used as type parameter 'TContext' in the generic type ormethod'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


启动类:
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?}");
});
}
}
}
实体配置:
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);
}
}
}

最佳答案

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


该消息告诉您,您的 UserContext类不继承自 DbContext ,这是强制性的。
它应该是这样的:
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options)
: base(options)
{ }

public DbSet<Blog> Blogs { get; set; }
}
如需更多信息,请参阅 EF Core TutorialConfiguring a DbContext .

关于c# - CS0311 C# 该类型不能用作泛型类型或方法中的类型参数 'TContext'。 Entity Framework 核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63016246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com