持久化相关条目时的OptimisticConcurrencyException

时间:2016-10-06 03:05:48

标签: c# entity-framework

固定

我需要阅读文档以了解它为什么修复错误,但添加

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

每个键自动增加之前解决问题。

抱歉,我读了很多这样的问题,但是我无法弄清楚发生了什么......

我只提出一个请求,所以我不是并发... 我有3个实体

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; }
}

第二

[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; }
}

无论我如何尝试添加SECOND或第三个实体,我都会得到

  

存储更新,插入或删除语句会影响意外的行数(0)。自实体加载后,实体可能已被修改或删除。有关理解和处理乐观并发异常的信息,请参阅http://go.microsoft.com/fwlink/?LinkId=472540

我在这里做错了什么?

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 });
}

注意:如果我只是尝试

db.mesa.Add(new Mesa(){nomeMesa ="asih"}); 
db.SaveChanges();

它坚持新实体。

已经阅读过问题:

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

和其他人......

0 个答案:

没有答案