从类库读取放置在ASP.NET Core中的连接字符串.数据库优先 [英] Reading connection string placed in ASP.NET Core from class library. Database First

查看:48
本文介绍了从类库读取放置在ASP.NET Core中的连接字符串.数据库优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目的结构如下:

  • 汽车(ASP.NET Core MVC.这里有一个连接字符串)

Cars.Persistence(ASP.NET核心类库.这里有存储库,数据库优先)

Cars.Persistence (ASP.NET Core Class library. Here we have Repository, Database First)

我已经通过

到目前为止,一切都很好.但是,现在 carsContext Cars.Persistence -ASP.NET Core Class库中具有硬编码的连接字符串:

So far so good. However, now carsContext has hard coded connection string in Cars.Persistence - ASP.NET Core Class library:

public partial class carsContext: DbContext
{
    public carsContext()
    {
    }

    public carsContext(DbContextOptions<carsContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Cars> Cars { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("Server=PC\SQL2014XP...");// hard coded 
                                                                  // connection string
        }
    }
}

起初,我想在我的类库 Cars.Persistence 中创建自己的 appsettings.json .但是,根据这篇文章,建议不要使用 appsettings.json 类库中的文件..

At first, I thought to create own appsettings.json in my class library Cars.Persistence. However, according to this post it is not advisable to have appsettings.json file in Class Library..

我已经阅读此方法,但是,如果我再次运行此命令,则硬编码字符串将再次出现:

I've read this approach, however, the hard coded string will appear again, if I will run this command again:

Scaffold-DbContext "Server=PC\SQL2014XP;Database=Cars;Trusted_Connection=True;" 
        Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

所以我的问题是如何在类库 Cars.Persistence 中使用连接字符串(位于 Cars 项目中)?

So my question is how can I use connection string(located in Cars project) in my class library Cars.Persistence?

更新:

我已注释掉以下代码:

public partial class eshopContext : DbContext
{

     public eshopContext(DbContextOptions<eshopContext> options): base(options)
     {} 

     // public eshopContext(){}        

      /*protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
     {
         if (!optionsBuilder.IsConfigured)
         {
             #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
             optionsBuilder.UseSqlServer("Server=...");
         }

      }*/

 }

推荐答案

您可以利用.Net Core依赖注入和开箱即用的功能.您的连接字符串将保留在Web项目中,但您可以使用数据库上下文,而无需在类库项目"中声明任何连接字符串.我正在共享现有项目中的代码示例.

You can take the advantage of .Net Core Dependency Injection and out of box features. Your connection string will remain in web project but you can use DB context without declaring any connection string in Class Library Project. I am sharing code sample from my existing project.

设置连接字符串

您已在启动时引用了连接字符串并将其添加到服务中.您无需再次定义连接字符串,也无需通过内置DI使用db上下文.代码看起来像这样!

You have referenced connection string in your start up and added to services. You don't need to define the connection string again and use the db context using Built in DI. The code could look like this !

开始上课

设置您的SQL配置.仔细看一下MigrationsAssembly,这是您引用类库项目的地方.

Set up your SQL config. Look closely at MigrationsAssembly this is where you would reference your class library project.

public static IServiceCollection AddCustomDbContext(this IServiceCollection services, IConfiguration configuration)
{

    // Add DbContext using SQL Server Provider
    services.AddDbContext<PaymentDbContext>(options =>
        options.UseSqlServer(configuration.GetConnectionString("myconnectionstring"), x => x.MigrationsAssembly("Payment.Persistence")));

    return services;
}

上下文类

该课程在您的课程库项目中.

This class is in your class library project.

public class PaymentDbContext : DbContext
    {
        public PaymentDbContext(DbContextOptions<PaymentDbContext> options)
            : base(options)
        {

        }

        public DbSet<Payments> Payments { get; set; }    


    }    

使用DI访问上下文

    private readonly PaymentDbContext _context;


     public PaymentsRepository(PaymentDbContext dbContext)
     {
     _context = dbContext;
    }

这篇关于从类库读取放置在ASP.NET Core中的连接字符串.数据库优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆