C# 使用Core和.Net framework 4.5.2的Web API。无法设置连接字符串
我有一个包含WebAPI核心启动项目的应用程序。 我发现我无法在.NETCore中为视图构建现有数据库。 因此,我决定为我的WebAPI核心应用程序使用.NETFramework,并在使用EF6的项目中获取数据。 但是在启动项目(WebAPI核心)中,我需要设置连接字符串。为此,我需要EF6,即使我在项目中添加了EF6,我似乎也无法做到这一点(也许在核心项目中无法做到这一点?)。 因此,当我尝试添加上下文时,该代码失败C# 使用Core和.Net framework 4.5.2的Web API。无法设置连接字符串,c#,entity-framework,asp.net-core,C#,Entity Framework,Asp.net Core,我有一个包含WebAPI核心启动项目的应用程序。 我发现我无法在.NETCore中为视图构建现有数据库。 因此,我决定为我的WebAPI核心应用程序使用.NETFramework,并在使用EF6的项目中获取数据。 但是在启动项目(WebAPI核心)中,我需要设置连接字符串。为此,我需要EF6,即使我在项目中添加了EF6,我似乎也无法做到这一点(也许在核心项目中无法做到这一点?)。 因此,当我尝试添加上下文时,该代码失败 public void ConfigureServices(IServ
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddMvcOptions(o => o.OutputFormatters.Add(
new XmlDataContractSerializerOutputFormatter()));
var connectionString = Startup.Configuration["connectionStrings:propertiesConnectionString"];
services.AddDbContext<SurveyEntities>(o => o.UseSqlServer(connectionString));
services.AddScoped<IPropertiesRepo, PropertiesRepo>();
}
public void配置服务(IServiceCollection服务)
{
services.AddMvcCore()
.addmvcopions(o=>o.OutputFormatters.Add(
新的XmlDataContractSerializerOutputFormatter());
var connectionString=Startup.Configuration[“connectionString:propertiesConnectionString”];
AddDbContext(o=>o.UseSqlServer(connectionString));
services.addScope();
}
如果我下载EF6,我会得到这个错误
错误CS0311类型“Properties.EF6.MSurveyV2Entities”不能为空
在泛型类型或方法中用作类型参数“TContext”
'EntityFrameworkServiceCollectionExtensions.AddDbContext(IServiceCollection,
行动,服务生命周期。没有
“Properties.EF6.MSurveyV2Entities”的隐式引用转换
至“Microsoft.EntityFrameworkCore.DbContext”
那么如何解决这个问题呢?您需要使用appsetings.json中存储的连接字符串。这是您最灵活地配置ASP.NET Core的方式 i、 e 之后在配置服务中
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyDatabase")));
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“MyDatabase”));
}
这里有更多信息和示例我在这里的链接中找到了答案。 如果链接中断,这是一个摘要; 在EF6项目中,我为db上下文放入了一个分部类,其中构造函数将连接字符串作为参数 公共部分类调查
{
public SurveyEntities(string connString) : base(connString)
{
}
}
在Web.API核心项目中,我已经在启动
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore();
var connectionString = Configuration.GetConnectionString("SurveyEntities");
services.AddScoped<SurveyEntities>(_ => new SurveyEntities(connectionString));
services.AddScoped<IPropertiesRepo, PropertiesRepo>();
}
对api的调用现在可以正常工作。出现了什么错误?错误CS0311类型“Properties.EF6.MSurveyV2Entities”不能用作泛型类型或方法“EntityFrameworkServiceCollectionExtensions.AddDbContext(IServiceCollection,Action,ServiceLifetime)”中的类型参数“TContext”。没有从“Properties.EF6.MSurveyV2Entities”到“Microsoft.EntityFrameworkCore.DbContext”的隐式引用转换。核心API似乎无法为EF6项目设置连接字符串。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore();
var connectionString = Configuration.GetConnectionString("SurveyEntities");
services.AddScoped<SurveyEntities>(_ => new SurveyEntities(connectionString));
services.AddScoped<IPropertiesRepo, PropertiesRepo>();
}
public class ContractController : Controller
{
private readonly IPropertiesRepo PropertiesRepo;
private readonly SurveyEntities db;
public ContractController(SurveyEntities _db,IPropertiesRepo repo)
{
this.db = _db;
this.PropertiesRepo = repo;
}
[HttpGet("getall")]
public IActionResult GetContracts()
{
var contracts = this.PropertiesRepo.GetAllLiveContracts().ToList();
return Ok(contracts);
}
}