asp.net core 2.0:connString 与“microsoft.entityframeworkcore.dbcontext”不兼容,因为它是一个字符串。无论如何要使它们兼容?

双子座

我正在将 ac# web 应用程序从 ASP.Net core 1.x 更改为 ASP.Net core 2.0。

public class Greetcontext : DbContext
{
    public Greetcontext(string connString) : base(connString){}
}

在以前的asp.net core 1.x 中,上面的曾经运行良好,但是在更改为asp.net core 2.0 时,我遇到了一个问题,由于Microsoft.EntityFrameWorkCore,connectionString 不能是字符串。于是,我改成了

public class Greetcontext : DbContext
{
    public Greetcontext(DbContextOptions<Greetcontext> connString) : base(connString){}
}

然而,它给出了一个错误,因为 appsettings.json 中的 connString 是字符串,使其彼此不兼容。无论如何这可以相互兼容吗?

塞伯

如果您将 EF Core 2.0 与 ASP.NET Core 2.0 一起使用,则应按如下所示配置 EF:

services.AddDbContext<TecTacDbContext>(opts =>
{
    // This is how you get the connectionString from your appsettings
    opts.UseSqlServer(Configuration.GetConnectionString("TecTac"));
    opts.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});

您可以在此处获取更多信息

塞伯

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档