【发布时间】:2021-01-17 04:20:07
【问题描述】:
晚安,我正在开发 WebApi,我使用的是代码优先的方法,为此我使用了实体框架 6.0.1,在我的项目中,我有以下类 PersonController.cs、Configurations.cs(我现在手动插入数据的地方)和我的 Person 类,我正在使用 Postman 来模拟请求,我在网站上查找了一些示例,但没有与我的示例相同,如果有人可以帮助我,谢谢。
PersonController.cs
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models;
using WebApi.Contexto;
namespace WebApi.Controllers
{
public class PersonController : ApiController
{
private readonly Context contexto = new Context();
[Route("api/person")]
[HttpGet]
public IHttpActionResult getAll(string search)
{
if (string.IsNullOrEmpty(search))
{
search = "";
}
var list = contexto.People.Where(x => x.firstName.Contains(search) || x.lastName.Contains(search));
return Ok(list);
}
}
}
Configurations.cs
namespace WebApi.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using WebApi.Models;
internal sealed class Configuration : DbMigrationsConfiguration<WebApi.Contexto.Context>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(WebApi.Contexto.Context context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
context.People.Add(new Person { firstName = "Andrew ", lastName = "Teste", id = 1, birthDate = new DateTime(2021, 01, 31) });
}
}
}
Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApi.Models
{
public class Person
{
public int id { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public DateTime birthDate { get; set; }
}
}
【问题讨论】:
-
您访问的具体网址是什么?
标签: c# .net entity-framework-6