简体   繁体   English

如何将.net框架数据层引用到.net核心控制台应用程序

[英]How reference .net framework data layer to .net core console app

I am new to .NET Core. 我是.NET Core的新手。 I already have a Web API application in .NET framework. 我已经在.NET Framework中拥有一个Web API应用程序。 This includes class library projects for my repository using Entity Framework. 这包括使用Entity Framework为我的存储库提供的类库项目。

Now I want to create a scheduler like service. 现在,我想创建一个类似于服务的调度程序。 I am planning to do it in .NET Core. 我计划在.NET Core中进行此操作。 For testing I created a sample .NET Core console application and added my old dll which contains my data layer code. 为了进行测试,我创建了一个示例.NET Core控制台应用程序,并添加了包含数据层代码的旧dll。

But when I run the console app, it shows an error as 'MyCon' not found. 但是,当我运行控制台应用程序时,它显示错误消息“ MyCon”。 Please help me with this. 请帮我解决一下这个。

This is my console app code: 这是我的控制台应用程序代码:

static void Main(string[] args)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

    IConfigurationRoot configuration = builder.Build();

    Console.WriteLine("Hello World!...Scheduler starting....");
    Scheduler scheduler = new Scheduler();
}

The scheduler code looks like the following 调度程序代码如下所示

public class Scheduler
{
    private readonly Timer _tmr = null;
    private readonly IUnitOfWork _unitOfWork = null;
    private static readonly HttpClient _httpClient = new HttpClient() { MaxResponseContentBufferSize = 1000000, Timeout = System.TimeSpan.FromSeconds(60) };

    public Scheduler()
    {
        try
        {
            _tmr = new Timer();
            _unitOfWork = new UnitOfWork();

            _tmr.Interval = 10000;
            _tmr.Elapsed += _tmr_Elapsed;
            _tmr.Start();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            _tmr = null;
        }
    }

    private void _tmr_Elapsed(object sender, ElapsedEventArgs e)
    {
        try
        {
            _tmr.Stop();

              /* My database interaction code here */

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            _tmr.Start();
        }
    }
 }

The following is my configuration code(appsettings.: 以下是我的配置代码(appsettings 。:

{
    "ConnectionStrings": {
        "MyCon": "Data Source=myconnectionstring;"
    }
}

I don't know is this enough for EF to work. 我不知道这足以使EF正常工作。

I got the following error; 我收到以下错误;

System.InvalidOperationException: No connection string named 'MyCon' could be found in the application config file. System.InvalidOperationException:在应用程序配置文件中找不到名为“ MyCon”的连接字符串。

at System.Data.Entity.Internal.LazyInternalConnection.Initialize() 在System.Data.Entity.Internal.LazyInternalConnection.Initialize()
at System.Data.Entity.Internal.LazyInternalConnection.get_Connection() 在System.Data.Entity.Internal.LazyInternalConnection.get_Connection()
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext 1.IsIdentityV1Schema(DbContext db)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext
在Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext 1.IsIdentityV1Schema(DbContext db)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext
1.IsIdentityV1Schema(DbContext db)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext
1.IsIdentityV1Schema(DbContext db)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext
1..ctor(String nameOrConnectionString, Boolean throwIfV1Schema)
1.IsIdentityV1Schema(DbContext db)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext
1..ctor(字符串名称OrConnectionString,布尔型throwIfV1Schema)

at MyProj.Data.Context.MyContextContext..ctor() 在MyProj.Data.Context.MyContextContext..ctor()
at MyProj.Repositories.UnitOfWork..ctor() 在MyProj.Repositories.UnitOfWork..ctor()
at Engager.Scheduler..ctor() in C:\\Users\\akhilraj.rajendran\\Documents\\Visual Studio 2017\\Projects\\Sample\\Sample\\Scheduler.cs:line 29} 在C:\\ Users \\ akhilraj.rajendran \\ Documents \\ Visual Studio 2017 \\ Projects \\ Sample \\ Sample \\ Scheduler.cs:line 29中的Engager.Scheduler..ctor()

I'm writing an answer since I do not have enough reputation to add a comment. 由于我没有足够的声誉来添加评论,因此我正在写一个答案。 From the information you provided it is not clear how you are reading the connection string. 根据您提供的信息,您不清楚如何读取连接字符串。 try adding below code in your Main function and check whether connection string is read or not from appsetting.json file. 尝试在您的Main函数中添加以下代码,并检查是否从appsetting.json文件中读取了连接字符串。

IConfigurationRoot configuration = builder.Build();
var connstring = configuration.GetConnectionString("MyCon");

Here is a great blog that will help you with reading appsettings.json in a .Net Core console application. 这是一个很棒的博客,它将帮助您阅读.Net Core控制台应用程序中的appsettings.json。

https://blog.bitscry.com/2017/05/30/appsettings-json-in-net-core-console-app/ https://blog.bitscry.com/2017/05/30/appsettings-json-in-net-core-console-app/

Now this is off topic, but, If you want to create a schedulers in .Net Core, you should definitely check out HangFire . 现在,这已成为话题,但是,如果要在.Net Core中创建调度程序,则一定要签出HangFire

Hangfire is an easy way to perform background processing in .NET and .NET Core applications. Hangfire是在.NET和.NET Core应用程序中执行后台处理的简便方法。 No Windows Service or separate process required. 无需Windows服务或单独的过程。 Backed by persistent storage. 由持久性存储支持。 Open and free for commercial use. 开放供商业使用。 Hangfire handles all the scheduling part so you can focus on writing jobs. Hangfire处理所有计划部分,因此您可以专注于编写作业。

暂无
暂无

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

相关问题 是否可以在将在Linux上运行的.Net Core Console App中使用.Net Framework 4.5 Dll参考 - Is it possible to use the .Net Framework 4.5 Dll reference in .Net Core Console App that will run in Linux 使用 ASP.NET Core 实体框架在数据层中进行数据加密 - Data Encryption in Data Layer with ASP.NET Core Entity Framework 为什么在 .NET Core 3.1 控制台应用程序中使用 Entity Framework Core 生成模型时会出现可为空的引用错误? - Why do I get nullable reference errors when generating a model using Entity Framework Core in a .NET Core 3.1 console app? 从.NET Core控制台应用程序内部将.NET Framework控制台应用程序启动到新窗口中 - Launch .NET Framework console app into a new window from inside a .NET Core Console app 如何在 .NET 框架的控制台应用程序中创建通知? - How to create notification in console app in .NET Framework? 如何在 .NET Core 项目中引用 .NET Framework 项目? - How do I reference a .NET Framework project in a .NET Core project? 如何使 .net 核心 3.1 控制台应用程序中的控制台记录器工作 - How to make console logger in .net core 3.1 console app work 如何使用实体框架在 .NET Core 6 中播种数据? - How to seed data in .NET Core 6 with Entity Framework? 从 .Net Framework 4.6.1 控制台应用程序转到 Core 2.0 控制台应用程序后出现 Newtonsoft DeserializeObject 错误 - Newtonsoft DeserializeObject error after going from .Net Framework 4.6.1 Console App to Core 2.0 console App 将数据从 .NET Core 控制台应用程序传递到 .NET Core Web 应用程序 - Pass data from .NET Core console app to .NET Core web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM