简体   繁体   English

如何使用依赖注入配置 Devart LinqConnect 数据库上下文

[英]How to configure the Devart LinqConnect Database Context with Dependency Injection

I have a Library project that I store all business logic for a Backend API service.我有一个库项目,它存储后端 API 服务的所有业务逻辑。 Included in that library is the Database Context provided by Devart.该库中包含 Devart 提供的数据库上下文。 How can I setup a Transient Service in my Startup.cs that will allow me to specify the connection string that I want to use when c# initiates the database with Dependency Injection?如何在我的 Startup.cs 中设置一个瞬态服务,允许我指定在 c# 使用依赖注入启动数据库时要使用的连接字符串? Here is some code with what I'm looking at and some of what I have tried.这是一些代码,其中包含我正在查看的内容以及我尝试过的一些内容。

I know that if you have a normal DbContext you can use the following:我知道,如果您有一个普通的 DbContext,您可以使用以下内容:

_ = services.AddDbContext<DbContext>(options => options.UseSqlServer(connString));

But because this is a devart data context I am not provided the options to do that.但是因为这是一个 devart 数据上下文,所以我没有提供这样做的选项。

I have tried creating a partial class:我尝试创建部分 class:

public partial class CustomDbContext : Library.DevartDbContext
    {
        public CustomDbContext() : base(dbConnectionString) { }
    }

Which this is just overriding the default Devart Db Context constructor, and this works, only If I am using the CustomDbContext class in my library, which I am not.这只是覆盖默认的 Devart Db Context 构造函数,并且只有当我在我的库中使用 CustomDbContext class 时才有效,而我不是。

Here is what the line looks like where I am injecting my DbContext currently and relying on the connection string to come from the library.这是我当前注入 DbContext 并依赖于来自库的连接字符串的行的样子。

_ = services.AddTransient<Library.DevartDbContext>();

I have not used Ninject before, would this be a good solution that could handle this for me?我以前没有使用过 Ninject,这是一个可以为我处理这个问题的好解决方案吗?

I can get my project to compile If I do this:我可以让我的项目编译如果我这样做:

_ = services.AddTransient(x => new Library.DevartDbContext(dbConnectionString));

are there any consequences for setting up my db context like this?像这样设置我的数据库上下文有什么后果吗? - EDIT Devart does not actually allow me to do this. - 编辑 Devart 实际上不允许我这样做。 I get an error with my connection string saying Key is not valid: license key.我的连接字符串出错,提示密钥无效:许可证密钥。 So I'm back to square 0.所以我回到方块 0。

Turns out, my solution of using事实证明,我使用的解决方案

_ = services.AddTransient(x => new Library.DevartDbContext(dbConnectionString));

was the correct solution, the errors I was getting were not related to using the Devart Data Context this way.是正确的解决方案,我得到的错误与以这种方式使用 Devart 数据上下文无关。 Because I had a previous database context that is still in the project and still being used, when I updated the connection string in my startup to point to the devart one that contains a license key in it, that's where my errors were coming from.因为我有一个以前的数据库上下文仍在项目中并且仍在使用,所以当我在启动时更新连接字符串以指向其中包含许可证密钥的 devart 时,这就是我的错误来源。

For the time being I now just supply two different connection strings, one with the license key and one without to be used with the selective database contexts.目前,我现在只提供两种不同的连接字符串,一种带有许可证密钥,另一种不用于选择性数据库上下文。

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

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