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

asp.net-core asp.net-core-mvc c# entity-framework-core

Question

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();
}
1
5
7/16/2018 2:46:32 AM

Popular Answer

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>
3
7/16/2018 4:22:19 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow