C# CS1061-不包含';Id';没有可访问的扩展方法';Id';接受类型的第一个参数
我正在尝试使用Blazor和Entity Framework Core制作一个CRUD应用程序,但在控制器页面中出错 以下是PeopleController.cs文件中的代码:C# CS1061-不包含';Id';没有可访问的扩展方法';Id';接受类型的第一个参数,c#,blazor,crud,blazor-server-side,C#,Blazor,Crud,Blazor Server Side,我正在尝试使用Blazor和Entity Framework Core制作一个CRUD应用程序,但在控制器页面中出错 以下是PeopleController.cs文件中的代码: using CuriousDriveTutorial.Data.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; usin
using CuriousDriveTutorial.Data.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace CuriousDriveTutorial.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class PeopleController : ControllerBase
{
//dependancy injection for applicationdbcontext
private readonly ApplicationDBContext context1;
public PeopleController(ApplicationDBContext context) { this.context1 = context; }
//get action method
[HttpGet]
public async Task<IActionResult> Get() => (IActionResult)await context1.People.ToListAsync();
//get action id
[HttpGet("{id}", Name = "GetPerson")]
public async Task<ActionResult<Person>> Get(int id) => await context1.People.FirstOrDefaultAsync(x => x.Id == id);
//create
[HttpPost]
public async Task<IActionResult> Post(Person person)
{
context1.Add(person);
await context1.SaveChangesAsync();
return Ok(person.Id);
}
//update
[HttpPut]
public async Task<IActionResult> Put(Person person)
{
context1.Entry(person).State = EntityState.Modified;
await context1.SaveChangesAsync();
return NoContent();
}
//delete
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
context1.Remove(new Person { Id = id });
await context1.SaveChangesAsync();
return NoContent();
}
}
}
您需要重命名Person对象中的ID字段,或者使用ID而不是ID
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
}
或
您确定您的PK没有命名为
PersonId
?您应该共享该类型的定义Person
。您可以为Person对象指定定义吗。Person.ID
与Person.ID
不同。C#区分大小写。投票结束时输入错误。已修复。我使用了你的最终解决方案。谢谢。请接受答案。
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
}
//get action id
[HttpGet("{id}", Name = "GetPerson")]
public async Task<ActionResult<Person>> Get(int id) => await context1.People.FirstOrDefaultAsync(x => x.ID == id)
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}