0
Using code-first generated the class:
namespace take8.Models
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("takeeat.pessoa")]
public partial class pessoa
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public pessoa()
{
pessoa_endereco = new HashSet<pessoa_endereco>();
}
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int id { get; set; }
[Column(TypeName = "char")]
[Required]
[StringLength(1)]
public string tipo { get; set; }
[Required]
[StringLength(100)]
public string razao_social { get; set; }
[StringLength(100)]
public string nome_fantasia { get; set; }
[StringLength(14)]
public string cpf_cnpj { get; set; }
[StringLength(15)]
public string rg_insc_estadual { get; set; }
public virtual ICollection<pessoa_endereco> pessoa_endereco { get; set; }
[JsonIgnore]
public virtual usuario usuario { get; set; }
[JsonIgnore]
public virtual vendedor vendedor { get; set; }
}
}
I created a controller with CRUD methods through the option: Api2 Web Controller with actions, using the Entity Framework, and the controller was generated:
namespace take8.Controllers
{
public class pessoasController : ApiController
{
private ModelTake db = new ModelTake();
// GET: api/pessoas
public IQueryable<pessoa> Getpessoa()
{
return db.pessoa;
}
// GET: api/pessoas/5
[ResponseType(typeof(pessoa))]
public IHttpActionResult Getpessoa(int id)
{
pessoa pessoa = db.pessoa.Find(id);
if (pessoa == null)
{
return NotFound();
}
return Ok(pessoa);
}
// PUT: api/pessoas/5
[ResponseType(typeof(void))]
public IHttpActionResult Putpessoa(int id, pessoa pessoa)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != pessoa.id)
{
return BadRequest();
}
db.Entry(pessoa).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!pessoaExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/pessoas
[ResponseType(typeof(pessoa))]
public IHttpActionResult Postpessoa(pessoa pessoa)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.pessoa.Add(pessoa);
try
{
db.SaveChanges();
}
catch (DbUpdateException)
{
if (pessoaExists(pessoa.id))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = pessoa.id }, pessoa);
}
// DELETE: api/pessoas/5
[ResponseType(typeof(pessoa))]
public IHttpActionResult Deletepessoa(int id)
{
pessoa pessoa = db.pessoa.Find(id);
if (pessoa == null)
{
return NotFound();
}
db.pessoa.Remove(pessoa);
db.SaveChanges();
return Ok(pessoa);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool pessoaExists(int id)
{
return db.pessoa.Count(e => e.id == id) > 0;
}
}
}
Ran all operations (GET, POST, PUT, DELETE) smoothly. But wanted to access the properties of the personal table
public virtual ICollection<pessoa_endereco> pessoa_endereco { get; set; }
What I need to do to access these properties?
The generator brought a list of addresses for a certain person, where do you want this list ??? in which method?
– novic
Thanks @Virgilio, but analyzing my question right, it doesn’t make much sense that I want to join these 2 tables, since a person can have several addresses, I saw that there is no need in my project of this, I will make independent queries.
– Gleyson Silva
Okay ... @Intenlider
– novic