FIXED
I need to read the documentation to see why it's fix the error, but adding
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
before each key that is AUTO INCREMENT fix the problem.
.
sorry about that, i read allot of questions like this I am doing, but i can't figure out what's happening...
I'm making only one request, so i'ts not concurrency ... I have 3 entitys
FIRST:
[Table("banco_bar.mesa")]
public partial class mesa
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public mesa()
{
mesatempedido = new HashSet<mesatempedido>();
pedidomesa = new HashSet<pedidomesa>();
}
[Key]
public int idMesa { get; set; }
[Required]
[StringLength(100)]
public string nomeMesa { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<mesatempedido> mesatempedido { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<pedidomesa> pedidomesa { get; set; }
}
SECOND:
[Table("banco_bar.mesatempedido")]
public partial class mesatempedido
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int idMesa { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int idPedidoMesa { get; set; }
[Key]
[Column(Order = 2)]
[StringLength(15)]
public string senhaPedido { get; set; }
[Key]
[Column(Order = 3)]
public bool requisitadoFechamento { get; set; }
public virtual mesa mesa { get; set; }
public virtual pedidomesa pedidomesa { get; set; }
}
THIRD:
[Table("banco_bar.pedidomesa")]
public partial class pedidomesa
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public pedidomesa()
{
itenspedido = new HashSet<itenspedido>();
mesatempedido = new HashSet<mesatempedido>();
}
[Key]
public int idPedidoMesa { get; set; }
public int? idMesa { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime dataAbertura { get; set; }
public DateTime? dataFechamento { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<itenspedido> itenspedido { get; set; }
public virtual mesa mesa { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<mesatempedido> mesatempedido { get; set; }
}
Doesn't matter how I try to add the SECOND or the THIRD Entity, i aways get
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.
What I am doing wrong here?
var db = new Models.ModelContext();
var mesa = db.mesa.Where(m => m.idMesa == mesaId).FirstOrDefault();
if (mesa == null)
{
return new HttpUnauthorizedResult();
}else
{
pedidomesa pm = db.pedidomesa.Add(new pedidomesa { mesa = mesa });
mesatempedido mtp = db.mesatempedido.Add(new mesatempedido { mesa = mesa, pedidomesa = pm, senhaPedido="bario" });
db.SaveChanges();
return Json(new { pm, mtp });
}
Note: If i just try
db.mesa.Add(new Mesa(){nomeMesa ="asih"});
db.SaveChanges();
it's persist the new entity.
Already read questions:
Does Entity Framework save related classes automatically?
Can Entity Framework add many related entities with single SaveChanges()?
How to update related entities in Entity Framework
Entity Framework Insert object with related object
and others ...