Question

On escalado, throws the exception. It throws with or wihtout Include.

static void Main(string[] args)
{
    try
    {                
        using (var context = new CKContext())
        {                    
            var servReprosWithIncludes = context.ServicioRepro
                                                .Include(p => p.Categoria)                                                        
                                                .ToList();
            var escalado = context.EscaladoPrecio
                                    //.Include(p => p.Servicio)
                                    .ToList();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

InvalidOperationException: The value of a property that is part of an object's key does not match the corresponding property value stored in the ObjectContext. This can occur if properties that are part of the key return inconsistent or incorrect values or if DetectChanges is not called after changes are made to a property that is part of the key.

The mapping of EscaladoPrecio:

public class EscaladoPrecioMapping : EntityTypeConfiguration<EscaladoPrecio>
{
    public EscaladoPrecioMapping()
    {
        base.HasKey(p => new { p.Desde, p.Hasta, p.ServicioReproId });            
        base.HasRequired(p => p.Servicio)
            .WithMany()
            .HasForeignKey(p => p.ServicioReproId);
        base.ToTable("PreciosServicioReprografia");
    }
}

The entity ServicioRepro is a part from TPT hierarchy. Looks like:

public class ServicioRepro : Producto
{        
    public bool IncluirPrecioClick { get; set; }

    public bool IncluirPrecioPapel { get; set; }

    public bool HayPapel { get; set; }

    public bool HayImpresion { get; set; }

    public bool PrecioPorVolumen { get; set; }

    //public virtual ICollection<EscaladoPrecio> EscaladoPrecio { get; set; }

    public virtual CategoriaServicioRepro Categoria { get; set; }        

    public virtual ServicioReproFacturacionType ServicioReproFacturacionType { get; set; }      
}

On this entity you can't see the key, because the base entity Producto have it.

The entity EscaladoPrecio have 3 PK: desde, hasta and Servicio. Servicio is PK and FK. The entity looks like (methods, overrides and members have been removed to reduce the code):

public class EscaladoPrecio : IComparable<EscaladoPrecio>, IComparable<int>, IComparable, IEntity
{
    #region Declarations

    private int _desde;
    private int _hasta;
    private double _precio;
    private int _cada;

    #endregion Declarations

    #region Constructor

    public EscaladoPrecio()
        : this(1, 1, 0, 0)
    { }

    public EscaladoPrecio(int desde, int hasta, double precio)
        : this(desde, hasta, precio, 0)
    { }

    public EscaladoPrecio(int desde, int hasta, double precio, int cada)
    {
        _desde = desde;
        _hasta = hasta;
        _precio = precio;
        _cada = cada;
    }

    #endregion Constructor

    #region Properties

    public int Desde
    {
        get
        {
            return _desde;
        }
        set
        {
            _desde = value;
        }
    }

    public int Hasta
    {
        get
        {
            return _hasta;
        }
        set
        {
            _hasta = value;
        }
    }

    public double Precio
    {
        get
        {
            return _precio;
        }
        set
        {
            _precio = value;
        }
    }

    public int Cada
    {
        get
        {
            return _cada;
        }
        set
        {
            _cada = value;
        }
    }

    #endregion Properties           

    private int _ServicioReproId;
    public int ServicioReproId
    {
        get
        {
            if (Servicio != null)
            {
                _ServicioReproId = Servicio.Id;
                return Servicio.Id;
            }
            else
                return 0;
        }
        set
        {
            _ServicioReproId = value;
        }

    }

    public virtual ServicioRepro Servicio { get; set; }
}

Why throws the exception?

Was it helpful?

Solution

Why are you doing this:

public int ServicioReproId
{
    get
    {
        if (Servicio != null)
        {
            _ServicioReproId = Servicio.Id;
            return Servicio.Id;
        }
        else
            return 0;
    }
    set
    {
        _ServicioReproId = value;
    }
}

Your part of the key property ServicioReproId is returning 0 here potentially although it has been loaded (and stored in the context) with a value != 0 (probably). I think this part of the exception is refering to this problem: "This can occur if properties that are part of the key return inconsistent or incorrect values."

Better leave it an automatic property:

public int ServicioReproId { get; set; }

OTHER TIPS

try to initialice his virtual property in the constructor of the class EscaladoPrecio()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top