Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

references are missing. Ensure project is referencing 'Microsoft.NET.Sdk.web' & PreserveCompilationContext property is not set to false

I am getting this error while trying to run the asp.net core project.

One or more compilation references are missing. Ensure project is referencing 'Microsoft.NET.Sdk.web' & PreserveCompilationContext property is not set to false

enter image description here

EfDemo.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>

</Project>

HomeController.cs

namespace EfDemo.Controllers
{
    public class HomeController : Controller
    {
        async public Task<IActionResult> Index()
        {
            using (var context = new EFCoreWebDemoContext())
            {
                var model = await context.Authors.AsNoTracking().ToListAsync();
                return View(model);
            }
        }
    }
}

EFCoreWebDemoContext.cs

public class EFCoreWebDemoContext : DbContext
{
    public DbSet<Book> Books { get; set; }
    public DbSet<Author> Authors { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
       optionsBuilder.UseSqlServer(@"connection string");
    }
 }

Index.cshtml

@{
    ViewData["Title"] = "Home Page";
}

<div>
    <span>Home Page</span>
</div>

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - EfDemo</title>
</head>
<body>
    @RenderBody()

    @RenderSection("Scripts", required: false)
</body>
</html>

_ViewImports.cshtml

@using EfDemo
@using EfDemo.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Microsoft.NET.Sdk

Added breakpoint and checked while executing the application it crashes on return View(model); in HomeController.cs file

Tried modifying the HomeController.cs shown below but still the same issue.

public IActionResult Index()
{
    return View();
}
like image 426
Sharath Avatar asked Jul 15 '18 19:07

Sharath


2 Answers

The Reference Versions are conflict.

I suggest you try modify csproj like below:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.3" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.4" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>
</Project>
like image 196
Edward Avatar answered Sep 23 '22 03:09

Edward


Updated csproj file with below packages and updated visual studio to 15.7.5 which wokred

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
  </ItemGroup>

</Project>
like image 44
Sharath Avatar answered Sep 21 '22 03:09

Sharath