Tuesday, 15 May 2012

c# - Entity Framework Code First 6 - Invalid Operation Exception when i put entity state ? Posible EF Bug? -



c# - Entity Framework Code First 6 - Invalid Operation Exception when i put entity state ? Posible EF Bug? -

i have problem entity framework code first 6 working disconnected. problem occurs when want mark entity modified, entity has association properties loaded new instance of dbcontext, more precise source of nail , save.

public list<venta> get(expression<func<venta, bool>> predicate) { seek { int num = 0; list<venta> ventas = new list<venta>(); using (_context = new mycontext(mycontext.getconnectionstring())) { if (predicate == null) ventas = _context.ventas.include("mesa").include("usuario").include("pago").tolist(); else ventas = _context.ventas.include("mesa").include("usuario").include("pago").where(predicate).tolist(); } //i utilize other repo load related entities usuariorepository userrepo = new usuariorepository(); foreach (venta item in ventas) { item.gettype().getproperty("usuario").setvalue(item, userrepo.get(u => u.id == item.usuarioid).first(), null); } //i utilize other repo load related entities productorepository prodrepo = new productorepository(); foreach (ventaproducto item in ventas.selectmany(vta => vta.detalle).tolist()) { producto p = prodrepo.get(prod => item.productoid == prod.id).first(); item.gettype().getproperty("producto").setvalue(item, p, null); } ventas.foreach(vta => vta.state = domainentitystate.unchanged); homecoming ventas; } grab (exception ex) { throw new exception("error al traer las ventas", ex); } } public override int save(venta venta) { int saves = 0; entitystate state; entitystate stateproducto; //new instance of context without entities in dbset's using (mycontext context = new mycontext(mycontext.getconnectionstring())) { seek { if (venta.isnewentity) //venta nueva { state = entitystate.added; stateproducto = entitystate.modified; } else { state = entitystate.modified; stateproducto = entitystate.modified; } //int usuarios = context.usuarios.local.count; //i 0 //int productos = context.productos.local.count; //i 0 //int ventasproductos = context.ventasproducto.local.count; // 0 venta.usuario = null; if (venta.pago != null) venta.pago.usuario = null; if (venta.pago != null) { entitystate estadopago = context.getentitystate(venta.pago); } //here problem context.setentitystate(venta, state); saves = context.savechanges(); } grab (exception ex) { throw new exception("error al grabar la venta", ex); } } homecoming saves; }

finally next error ....

attaching entity of type 'jackie.domain.entities.usuario' failed because entity of same type has same primary key value. can happen when using 'attach' method or setting state of entity 'unchanged' or 'modified' if entities in graph have conflicting key values. may because entities new , have not yet received database-generated key values. in case utilize 'add' method or 'added' entity state track graph , set state of non-new entities 'unchanged' or 'modified' appropriate.

the unusual if load entities same instance of context not have problem. bug? no longer think of more. give thanks all. made google translator.

there (at least) 2 usuario properties in object graph - venta.usuario , venta.pago.usuario. attaching venta entity, entity objects in these 2 usuario properties attached unmodified. if these 2 properties hold 2 different instances of usuario class same pk value, when sec attached throw error received. seems quite possible you're loading navigation properties via different instances of context eager loading thrown mix.

break @ phone call context.setentitystate(venta, state) , see if 2 usuario objects same instance or 2 different instances same pk value(s).

c# entity-framework ef-code-first code-first

No comments:

Post a Comment