简体   繁体   English

.NET Core - 针对旧的 .NET 框架

[英].NET Core - Targetting old .NET framework

What I want to do: use an older project (.NET 3.5) data context and repositories in a .net core application (targetting .net 4.5).我想要做的是:在 .net 核心应用程序(针对 .net 4.5)中使用旧项目(.NET 3.5)数据上下文和存储库。 I put a more detailed explanation and what I tried below.我在下面给出了更详细的解释和我尝试过的内容。

What I want to do我想做什么

I am building a MVC application using .NET Core (on .NET 4.6 framework) at my work.我正在工作中使用 .NET Core(在 .NET 4.6 框架上)构建一个 MVC 应用程序。 The application needs to integrate with an existing database.应用程序需要与现有数据库集成。

Normally in my .NET Core MVC applications, in startup.cs I do something like:通常在我的 .NET Core MVC 应用程序中,在startup.cs我会执行以下操作:

services.DbContext<Context>(options...) and parse a connection string. services.DbContext<Context>(options...)并解析连接字符串。 I then do services.AddTransient ... and inject a repository to my controllers, which lets me access the database.然后我执行services.AddTransient ... 并将存储库注入我的控制器,这让我可以访问数据库。

However, instead of setting up my own database context and repositories and queries, I would like to use the existing codebase at my work.但是,我想在工作中使用现有的代码库,而不是设置我自己的数据库上下文、存储库和查询。 I created my project in the same solution as the existing ones.我在与现有解决方案相同的解决方案中创建了我的项目。 The existing projects are web forms, and use .NET 3.5.现有项目是 Web 表单,并使用 .NET 3.5。 The way they do the database and repositories is a bit different - it uses DataContext in System.Data.Linq , and reads a connection string from web.config .他们处理数据库和存储库的方式有点不同——它使用System.Data.Linq DataContext ,并从web.config读取连接字符串。 Instead of dependency injection, then, the controllers just instantiate an instance of this class.然后,控制器只是实例化此类的一个实例,而不是依赖注入。

What I have tried我试过的

In my startup.cs , I tried to create a DataContext and parse it into the other project's repository, but it said something like You need to reference System.Data.Linq in the .NET framework 3.5.0 .在我的startup.cs ,我尝试创建一个 DataContext 并将其解析到另一个项目的存储库中,但它说类似You need to reference System.Data.Linq in the .NET framework 3.5.0

Is it possible for a .NET Core project targeting .NET 4.5, to use data context classes and repositories built using .NET 3.5?面向 .NET 4.5 的 .NET Core 项目是否可以使用使用 .NET 3.5 构建的数据上下文类和存储库?

I actually solved my problem.我实际上解决了我的问题。 I don't think I was clear enough.我觉得我说的还不够清楚。 Here is what my solution is like:这是我的解决方案:

Solution - .NET 3.5 project - .NET 4.5 project解决方案 - .NET 3.5 项目 - .NET 4.5 项目

I was creating a repository from .NET 3.5 project in my newer one, and it was creating a datacontext that looks for a web.config file.我在较新的项目中从 .NET 3.5 项目创建了一个存储库,它正在创建一个查找web.config文件的数据上下文。 Even though I created one, it looks like it must try to find that web.config inside of the startup project, regardless of where the data context is created.即使我创建了一个,它看起来也必须尝试在启动项目中找到web.config ,无论数据上下文是在哪里创建的。 I made one but it still didn't detect that, I'm not sure why.我做了一个,但它仍然没有检测到,我不知道为什么。

I was able to go into the old project and modify the datacontext to directly received a connection string.我能够进入旧项目并修改数据上下文以直接接收连接字符串。 However, this isn't ideal - I will look for a way to pass a connection string into the older project from my new one, which I think is a good solution.但是,这并不理想 - 我将寻找一种方法将连接字符串从我的新项目传递到旧项目,我认为这是一个很好的解决方案。

@jwdeny was correct in saying I had to add a reference to System.Data.Linq. @jwdeny 说我必须添加对 System.Data.Linq 的引用是正确的。 The project told me to add a reference to 3.5, but adding System.Data.Linq 4.0 was enough.项目告诉我添加对3.5的引用,但添加System.Data.Linq 4.0就足够了。

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

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