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). 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. The application needs to integrate with an existing database.
Normally in my .NET Core MVC applications, in startup.cs
I do something like:
services.DbContext<Context>(options...)
and parse a connection string. I then do services.AddTransient
... and inject a repository to my controllers, which lets me access the database.
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. 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
. 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
.
Is it possible for a .NET Core project targeting .NET 4.5, to use data context classes and repositories built using .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
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. 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. 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. The project told me to add a reference to 3.5, but adding System.Data.Linq 4.0 was enough.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.