0

在 escalado 上,抛出异常。它抛出或不包括在内。

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:作为对象键一部分的属性值与存储在 ObjectContext 中的相应属性值不匹配。如果作为键的一部分的属性返回不一致或不正确的值,或者在对作为键的一部分的属性进行更改后未调用 DetectChanges,则可能会发生这种情况。

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

实体 ServicioRepro 是 TPT 层次结构的一部分。好像:

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

在这个实体上你看不到密钥,因为基础实体 Producto 有它。

实体 EscaladoPrecio 有 3 个 PK:desde、hasta 和 Servicio。Servicio是PK和FK。实体看起来像(方法、覆盖和成员已被删除以减少代码):

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

为什么会抛出异常?

4

2 回答 2

0

尝试在类 EscaladoPrecio() 的构造函数中初始化他的虚拟属性

于 2013-07-12T10:55:49.150 回答
0

你为什么做这个:

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

您的 key 属性部分可能ServicioReproId会返回0此处,尽管它已被加载(并存储在上下文中)一个值!= 0(可能)。我认为这部分异常是指这个问题:“如果作为键的一部分的属性返回不一致或不正确的值,就会发生这种情况。

最好将其保留为自动属性:

public int ServicioReproId { get; set; }
于 2013-07-12T21:04:24.150 回答