嗨,我正在尝试与一个人以及与此人相关的技能返回 JSON。
我有班人
public class Person
{
[Key]
public int personID { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
public string role { get; set; }
public virtual ICollection<Skill> Skills { get; set; }
}
还有一个职业技能
public class Skill
{
[Key]
public int skillID { get; set; }
public int personID { get; set; }
public string title { get; set; }
public virtual Person Person { get; set; }
}
数据库上下文
public class ConsultantContext : DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<Skill> Skill { get; set; }
}
我用一些数据填充了upp。
公共类PersonInit:DropCreateDatabaseIfModelChanges {受保护的覆盖无效种子(ConsultantContext上下文){
var skills = new List<Skill>
{
new Skill { skillID = 1, personID = 1, title = "Javascript" },
new Skill { skillID = 2, personID = 1, title = "jQuery" },
new Skill { skillID = 3, personID = 1, title = "ASP.NET" }
};
skills.ForEach(s => context.Skills.Add(s));
context.SaveChanges();
var persons = new List<Person>
{
new Person { personID = 1, firstName = "Name1", lastName = "Lastname1", age = 30, role = "Programmer" },
new Person { personID = 2, firstName = "Name2", lastName = "Lastname2", age = 30, role = "Designer" },
new Person { personID = 3, firstName = "Name3", lastName = "Lastname3", age = 30, role = "Manager" }
};
persons.ForEach(s => context.Persons.Add(s));
context.SaveChanges();
}
}
我有一个名为 Person 的控制器
public class PersonController : ApiController
{
private ConsultantContext db = new ConsultantContext();
// GET api/Person
public IEnumerable<Person> GetPeople()
{
return db.Persons.AsEnumerable();
}
// GET api/Person/5
public Person GetPerson(int id)
{
Person person = db.Persons.Find(id);
if (person == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return person;
}
// PUT api/Person/5
public HttpResponseMessage PutPerson(int id, Person person)
{
if (ModelState.IsValid && id == person.personID)
{
db.Entry(person).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
// POST api/Person
public HttpResponseMessage PostPerson(Person person)
{
if (ModelState.IsValid)
{
db.Persons.Add(person);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, person);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = person.personID }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
// DELETE api/Person/5
public HttpResponseMessage DeletePerson(int id)
{
Person person = db.Persons.Find(id);
if (person == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
db.Persons.Remove(person);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK, person);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
所以现在解决问题,当我在提琴手中使用 http:/localhost/api/Person 时,它会返回 JSON:
它返回一个空的 []。
有人可以帮我找回带有 personID、姓名、姓氏、角色和技能 [属于此人] 的 JSON。
问候