gpt4 book ai didi

c# - 缺少引用。确保项目引用 'Microsoft.NET.Sdk.web' & PreserveCompilationContext 属性未设置为 false

转载 作者:太空狗 更新时间:2023-10-29 23:20:01 31 4
gpt4 key购买 nike

我在尝试运行 asp.net 核心项目时遇到此错误。

缺少一个或多个编译引用。确保项目引用“Microsoft.NET.Sdk.web”并且 PreserveCompilationContext 属性未设置为 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

添加断点并在执行应用程序时检查它在返回 View(model) 时崩溃;在 HomeController.cs 文件中

尝试修改如下所示的 HomeController.cs 但仍然是同样的问题。

public IActionResult Index()
{
return View();
}

最佳答案

引用版本有冲突。

我建议你尝试修改 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.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>

关于c# - 缺少引用。确保项目引用 'Microsoft.NET.Sdk.web' & PreserveCompilationContext 属性未设置为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51351313/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com