none
Query InvalidOperationException RRS feed

  • Question

  • Hello Group:

    I'm starting with the Entity Framework, when I try to run the following query:

    Dim person = (From p In Me.TypedContext.Movimientos.OfType(Of Persona)() Where p.Codigo = codigo).SingleOrDefault

    returns the following exception:

    Ocurrió System.InvalidOperationException
      Message=Se ha infringido la restricción de multiplicidad. El rol 'PersonasFisica' de la relación 'TesoreriaModel.PersonasFisicaPersona' tiene una multiplicidad de 1 ó 0..1.
      Source=System.Data.Entity
      StackTrace:
           en System.Data.Objects.EntityEntry.WillNotRefSteal(EntityReference refToPrincipal, IEntityWrapper wrappedPrincipal)
           en System.Data.Objects.EntityEntry.FixupEntityReferenceToPrincipal(EntityReference relatedEnd, EntityKey foreignKey, Boolean setIsLoaded, Boolean replaceExistingRef)
           en System.Data.Objects.EntityEntry.FixupReferencesByForeignKeys(Boolean replaceAddedRefs)
           en System.Data.Objects.ObjectStateManager.FixupReferencesByForeignKeys(EntityEntry newEntry, Boolean replaceAddedRefs)
           en System.Data.Objects.ObjectStateManager.AddEntry(IEntityWrapper wrappedObject, EntityKey passedKey, EntitySet entitySet, String argumentName, Boolean isAdded)
           en System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
           en lambda_method(Closure , Shaper )
           en System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
           en System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
           en System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
           en System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence)
           en System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
           en System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
           en System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
           en TesoreriaGui.GUI.Personas.GetRow(Int32 codigo) en C:\Users\alejandro\Desarrollo\Tesoreria2\TesoreriaGui\GUI.Personas.vb:línea 592
      InnerException:

    Attached file with a part of the model.
    Basically I have a Movimiento entity, an entity Person which inherits Movimiento and other entity, PersonaFisica that has a one on one relation with Person

    thanks
    Monday, July 30, 2012 8:28 PM

All replies

  • Hi Alejandro,

    Welcome to the MSDN forum

    Could you please let me know the relationship between PersonasFisica and PersonasFisicaPersona?

    Do you use code first? If so, please post some code snippet about relationship settings.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, August 1, 2012 1:55 AM
  • Hi, I have the same problem

    my objects are

    public class Producto
        {
            public int Id { get; set; }
            [Required]
            [MaxLength(50)]
            public string Codigo { get; set; }
            [Required]
            [MaxLength(150)]
            public string Descripcion { get; set; }
            public decimal Ancho { get; set; }
            public decimal Largo { get; set; }
            public int Gramaje { get; set; }
            public bool Habilitado { get; set; }
            public string CodigoBarra { get; set; }
            
            public int? UnidadMedidaID { get; set; }
            public virtual UnidadMedida UnidadMedida { get; set; }

            public decimal PrecioLista { get; set; }

            public decimal PrecioCliente { get; set; }
            public decimal PrecioSucursal { get; set; }

            //[Column(TypeName = "datetime2")]
            public DateTime? PrecioVigenciaDesde { get; set; }
            //[Column(TypeName = "datetime2")]
            public DateTime? PrecioVigenciaHasta { get; set; }                     
            //[Column(TypeName = "datetime2")]
            public DateTime? PrecioVigenciaDesdeCliente { get; set; }
            //[Column(TypeName = "datetime2")]
            public DateTime? PrecioVigenciaHastaCliente { get; set; }        
            //[Column(TypeName = "datetime2")]
            public DateTime? PrecioVigenciaDesdeSucursal { get; set; }
            //[Column(TypeName = "datetime2")]
            public DateTime? PrecioVigenciaHastaSucursal { get; set; }

            public virtual ICollection<Precio> PreciosLista { get; set; }
            public virtual ICollection<PrecioSucursal> PreciosSucursal { get; set; }
            public virtual ICollection<PrecioCliente> PreciosCliente { get; set; }

            public DateTime FechaAlta { get; set; }
            public DateTime FechaModificacion { get; set; }
            public int UsuarioAltaID { get; set; }
            public int UsuarioModificacionID { get; set; }

            public virtual Usuario UsuarioAlta { get; set; }
            public virtual Usuario UsuarioModificacion { get; set; }

            [Required]
            public int TipoProductoID { get; set; }
            public virtual TipoProducto TipoProducto { get; set; }

            [Required]
            public int SubTipoProductoID { get; set; }
            public virtual SubTipoProducto SubTipoProducto { get; set; }

            public virtual ICollection<ProductoDeposito> ProductoDepositos { get; set; }
            public virtual ICollection<StockMovimiento> Movimientos { get; set; }

            [ConcurrencyCheckAttribute]
            [Timestamp]
            public Byte[] Timestamp { get; set; }
        }

    public class ProductoDeposito
        {
            public int ProductoID { get; set; }
            [ForeignKey("ProductoID")]
            public virtual Producto Producto { get; set; }
            public int DepositoID { get; set; }
            [ForeignKey("DepositoID")]
            public virtual Deposito Deposito { get; set; }
            public int Cantidad { get; set; }
        }

    public class Deposito
        {
            public int Id { get; set; }
            public string Codigo { get; set; }
            public string Descripcion { get; set; }
            public string Ubicacion { get; set; }


            public int? UsuarioAltaID { get; set; }
            public int? UsuarioModificacionID { get; set; }

            [ForeignKey("UsuarioAltaID")]
            public virtual Usuario UsuarioAlta { get; set; }
            [ForeignKey("UsuarioModificacionID")]
            public virtual Usuario UsuarioModificacion { get; set; }

            [ConcurrencyCheckAttribute]
            [Timestamp]
            public Byte[] Timestamp { get; set; }

            //Auditoría
            public DateTime? FechaAlta { get; set; }
            public DateTime? FechaModificacion { get; set; }

            public virtual ICollection<ProductoDeposito> ProductoDepositos { get; set; }
            public virtual ICollection<StockMovimiento> Movimientos { get; set; }
        }

    and in the context class I put

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                //Evitar pluralizar las tablas
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
             
                modelBuilder.Entity<ProductoDeposito>()
                  .HasKey(cp => new { cp.ProductoID, cp.DepositoID });

                modelBuilder.Entity<Producto>()
                            .HasMany(c => c.ProductoDepositos)
                            .WithRequired()
                            .HasForeignKey(cp => cp.ProductoID);

                modelBuilder.Entity<Deposito>()
                            .HasMany(p => p.ProductoDepositos)
                            .WithRequired()
                            .HasForeignKey(cp => cp.DepositoID);  
            }

    In my case the error occur when I'm trying to save changes over the lines

    db.Entry(prod).State = EntityState.Modified; -> here the error occurr....
    db.Producto.Attach(prod);

    Thanks.

    Tuesday, July 9, 2013 8:39 PM