C# 无法通过实体框架查询SQL Server;没有为此DbContext配置数据库提供程序;
我要运行此查询:C# 无法通过实体框架查询SQL Server;没有为此DbContext配置数据库提供程序;,c#,sql-server,entity-framework,C#,Sql Server,Entity Framework,我要运行此查询: public IList<User> GetList() { using (var context = new MssqlContext()) { // this throws the exception var query = from h in context.Users select h; var user
public IList<User> GetList()
{
using (var context = new MssqlContext())
{
// this throws the exception
var query = from h in context.Users
select h;
var users = query.ToList<User>();
return users;
}
}
“可以通过重写DbContext.onConfigurang方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序”。我看到的只是一个从未使用过的私有连接字符串属性。S.a.提供程序位于连接字符串中。所以错误是说您没有有效的连接字符串。好的,谢谢,我会调查的it@jdweng我的连接字符串如下所示:“Data Source=DESKTOP-91JG566;Initial Catalog=db_SongSandVoces;Integrated Security=true”我应该使用其他表单吗?如果数据库(MDF文件)连接到SQL Server,则该表单是正确的。使用SQL Server Management Studio查询数据库。检查SSMS的登录窗口。应该是窗口凭证。服务器名称和实例(服务器/实例)应为用于数据源的名称和实例。
public class MssqlContext : DbContext
{
private readonly string _connectionString;
public DbSet<User> Users { get; set; }
public MssqlContext()
{
// Temporary solution.
var builder = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true);
var config = builder.Build();
//_connectionString = ConfigurationManager;
_connectionString = config["ConnectionStrings:SAVConnectionString"];
}
}