0

在escalado上引发异常。它抛出或没有包括。实体框架:使用组合键作为PK/FK的实体抛出异常

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

为什么抛出异常?

回答

0

的构造你为什么这样做:

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

你的关键属性ServicioReproId这里返回0潜在尽管它的一部分已加载(并存储在上下文中),值为!= 0(可能)。我觉得异常的这部分指的这个问题:“可能发生这种情况如果是关键部分返还不一致或不正确的值的属性。”

最好不要它自动属性:

public int ServicioReproId { get; set; } 
+0

解决了包括VeGo答案在内的问题。谢谢。 – user2575650

0

尝试initialice他的虚拟财产类EscaladoPrecio()

+0

谢谢Vego,你的回答帮助我解决了错误。 – user2575650

相关问题