2017-10-17 59 views
0

我正在改变一个C#web应用程序从ASP.Net核心1.x到ASP.Net核心2.0。asp.net core 2.0:connString与'microsoft.entityframeworkcore.dbcontext'不兼容,因为它是一个字符串。无论如何,使他们兼容?

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

在以前的asp.net核心1.x中,上述用于运行很好,但同时改变成asp.net 2.0核心,我运行到一个问题,即的connectionString不能因为微软的字符串.EntityFrameWorkCore。因此,我改为

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

但是,由于appsettings.json中的connString是字符串,所以它给出了一个错误,使得它互相不兼容。无论如何,这可以互相兼容吗?

回答

0

如果您正在使用EF核心2.0 ASP.NET 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); 
}); 

你可以得到更多的信息here

勒布