0

こんにちは、ある人物とその人物に関連するスキルを含む 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; }
}

DB コンテキスト

    public class ConsultantContext : DbContext
{
    public DbSet<Person> Persons { get; set; }
    public DbSet<Skill> Skill { get; set; }

}

そして、いくつかのデータを入力します。

public class PersonInit : DropCreateDatabaseIfModelChanges { protected override void Seed(ConsultantContext context) {

        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、name、lastname、role、skills を含む JSON を取得するのを手伝ってくれる人はいますか?

よろしく

4

1 に答える 1

0

これを試して

    public IEnumerable<Person> GetPeople()
    {
        var skills = db.Skill;
        var people = db.Persons;
        people.ForEach(person => person.Skills = skills.FindAll(skill => skill.personID == person.personID));

        return people;
    }
于 2012-10-30T19:23:42.030 に答える