【问题标题】:Web API using Core and .Net framework 4.5.2. CAnnot set connection string使用 Core 和 .Net 框架 4.5.2 的 Web API。不能设置连接字符串
【发布时间】:2017-09-22 04:08:40
【问题描述】:

我有一个包含 Web API Core 启动项目的应用程序。 我发现我无法在 .Net Core 中为视图搭建现有数据库。 因此,我决定为我的 Web API Core 应用程序使用 .Net 框架并使用 EF6 获取项目中的数据。 但是在启动项目(Web API Core)中,我需要设置连接字符串。为此,我需要 EF6,即使我在项目中添加 EF6,我似乎也无法做到这一点(也许这不能在核心项目中完成?)。 所以这段代码在我尝试添加上下文的地方失败了;

   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>();

    }

如果我下载 EF6,我会收到此错误;

错误 CS0311 类型“Properties.EF6.MSurveyV2Entities”不能 在泛型类型或方法中用作类型参数“TContext” 'EntityFrameworkServiceCollectionExtensions.AddDbContext(IServiceCollection, 行动,服务寿命)'。没有 来自“Properties.EF6.MSurveyV2Entities”的隐式引用转换 到“Microsoft.EntityFrameworkCore.DbContext”。

那么我该如何解决这个问题?

【问题讨论】:

  • 你得到什么错误?
  • 错误 CS0311 类型“Properties.EF6.MSurveyV2Entities”不能用作泛型类型或方法“EntityFrameworkServiceCollectionExtensions.AddDbContext(IServiceCollection, Action, ServiceLifetime) 中的类型参数“TContext” )'。没有从“Properties.EF6.MSurveyV2Entities”到“Microsoft.EntityFrameworkCore.DbContext”的隐式引用转换。
  • Core API 似乎无法为 EF6 项目设置连接字符串。

标签: c# entity-framework asp.net-core


【解决方案1】:

我在此链接中找到了答案。 https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6 如果链接断开,这是一个摘要; 在 EF6 项目中,我为 db 上下文放置了一个部分类,其中构造函数将连接字符串作为参数 公共部分类SurveyEntities

{
    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>();

    }

在我的控制器中

   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);
        }
    }

对 api 的调用现在可以工作了。

【讨论】:

    【解决方案2】:

    您需要使用存储在 appsettings.json 中的连接字符串。这是您最灵活地配置 ASP.NET Core 的方式。

    {
      "ConnectionStrings": {
          "MyDatabase": "Server (localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
       },
    }
    

    之后在配置服务中

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<MyDbContext>(options =>    
        options.UseSqlServer(Configuration.GetConnectionString("MyDatabase")));
    }
    

    你有更多的信息和例子在这里https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 2019-01-07
      • 2020-01-07
      • 1970-01-01
      • 2014-07-17
      • 2020-04-14
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      相关资源
      最近更新 更多