I have MVC application which contains a class called DataAccess that retrieves data form a database. I added asp.Net Core application and I referenced the MVC application in the asp.Net Core application. The problem is that when I call one of the DataAccess methods and compile the solution, I get an error:
Error CS0012 The type 'DataTable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
See below code for my .net Core API:
using Microsoft.AspNetCore.Mvc;
using shopping;
using System.Collections.Generic;
using System.Data.Common;
namespace ShoppingAPI.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IActionResult Get()
{
DataAccess da = new DataAccess();
da.GetDataTable("SELECT * FROM USERS;", new List<DbParameter>());
return Ok();
}
}
Can anyone advise me?
Step 1. Target the correct framework
With the assumption that you are using Visual Studio 2017 here is how you can solve your problem.
Right-click on your project and select Edit *****.csproj
In the .csproj
file, you need to replace the target framework to the .NET Framework.
for example:
// incorrect <TargetFramework>netcoreapp2.0</TargetFramework> // correct <TargetFramework>net462</TargetFramework>
source: https://docs.microsoft.com/en-us/dotnet/standard/net-standard
For a list of the Target Framework Moniker (TFM) (ie, net47
, netstandard2.0
, netcoreapp2.0
, etc.*) you can check this link out: https://docs.microsoft.com/en-us/dotnet/standard/frameworks
Step 2. Run dotnet restore
Go to your output window and run
dotnet restore
.
Sometimes Visual Studio may misbehave (depending on which update you have installed), so you may have to close and re-open your Visual Studio. Otherwise, sometimes a clean/re-build may do the trick.
According to your comments, you noted that you were trying to reference a project that was MVC4, which means it was likely not written using a library that is supported directly with the .NET Core framework. Instead, it would be supported by the .NET Framework (w/ .NET Core) instead.
The solution above just does that for you.
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.