简体   繁体   English

.NET core - 将连接字符串读入 Entity Framework Core

[英].NET core - read connection string into Entity Framework Core

My project structure is pretty standard:我的项目结构非常标准:

项目结构

within EFData .EFData

EFData is an Entity Framework Core class library that isolates all of the database interaction. EFData是一个 Entity Framework Core 类库,它隔离了所有数据库交互。 Database models, and my DBContext .数据库模型和我的DBContext I built it that way so that it's database environment agnostic.我以这种方式构建它,因此它与数据库环境无关。

The API project of course has a reference to EFData . API 项目当然有对EFData的引用。

How do I pass the connection string from Startup.cs in API to DBContext.cs in EFData ?如何将连接字符串从API Startup.cs传递到DBContext.cs中的EFData

I've read multiple articles that reference a set up different than mine where I might expect a Startup.cs in my EFData project.我阅读了多篇文章,这些文章引用的设置与我的设置不同,我可能希望在我的EFData项目中使用Startup.cs I don't have that.我没有那个。

Within Startup.cs , I do have what I thought was the required line -Startup.cs ,我确实有我认为需要的行 -

services.AddDbContext<DBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

But EFData knows nothing about that connection string.但是EFData对该连接字符串一无所知。

One approach can be adding a ConnectionString property in the DBContext.cs class and then setting it on the startup.cs of the API project explicitly.一种方法是在 DBContext.cs 类中添加 ConnectionString 属性,然后在 API 项目的 startup.cs 上显式设置它。 Add the following code in DBcontext.cs在 DBcontext.cs 中添加以下代码

   using System;
   using System.Data.SqlClient;

   using Microsoft.EntityFrameworkCore;

   public class DBContext 
   {
       public static void SetConnectionString(string connectionString)
       {
           if (ConnectionString == null)

           {
               ConnectionString = connectionString;
           }
           else
           {
               throw new Exception();
           }
       }
       // this part will help you to open the connection
       public static SqlConnection OpenConnection()
       {
           SqlConnection connection = new SqlConnection(ConnectionString);
           connection.Open();
           return connection;
       }

       private static string ConnectionString { get; set; }

       //add the connectionString to options

         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
       {
           if (!optionsBuilder.IsConfigured)
           {
               optionsBuilder.UseSqlServer(ConnectionString);
           }
       }
   }

Now, in the API project add a reference of the EFData project and in the startup.cs file set the ConnectionString现在,在 API 项目中添加 EFData 项目的引用,并在 startup.cs 文件中设置 ConnectionString

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            var connectionString = this.Configuration.GetConnectionString("DBName");
            Namespace.DBContextContext.SetConnectionString(connectionString); //replace namespace with the namespace suitable for your solution

            //here goes rest of your default code
        }

This way you should be able to access the Connection in your API project.通过这种方式,您应该能够访问 API 项目中的 Connection。

Steps to Get Connection and build your database获取连接和构建数据库的步骤

  1. Add reference to EFData Project in API project - do this by in the API project, clicking on the dependecies section and adding reference to your EFData class library.在 API 项目中添加对 EFData 项目的引用 - 在 API 项目中执行此操作,单击依赖项部分并添加对 EFData 类库的引用。
  2. Then add to your startup class in your api project and ensure the connection string is in your ConfigureServices method or similar然后添加到您的 api 项目中的启动类并确保连接字符串在您的 ConfigureServices 方法或类似方法中

your connection string looks good just make sure your database connection string is correct.您的连接字符串看起来不错,只需确保您的数据库连接字符串正确即可。

Here is a link if you need more information如果您需要更多信息, 这里是一个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM