Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

962
Vistas
Foreign key created in shadow and appending random 1 to column name - ASP:NET EF Core

When I migrate my new models and data I get the following error for multiple foreign keys:

The foreign key property 'InsurancePolicy.InsuranceSubjectID1' was created in shadow state because a conflicting property with the simple name 'InsuranceSubjectID' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type.

The weird thing is that I define my relationships the same throughout all models, but some work okay (FK without 1 is stored) and some don't.

Example of my models:

public class InsurancePolicy
{
    public int InsurancePolicyID { get; set; }
    [DataType(DataType.Currency)]
    [Column(TypeName = "money")]
    public decimal FinalSum { get; set; }
    public DateTime DateFrom { get; set; }
    public DateTime DateTo { get; set; }
    public int? InsuredID { get; set; }
    public Insured? Insured { get; set; }
    public int? InsuranceSubjectID;
    public InsuranceSubject? InsuranceSubject { get; set; }
    public int? InsuranceSubtypeID;
    public InsuranceSubtype? InsuranceSubtype { get; set; }
}

public class InsuranceSubject
{
    public int InsuranceSubjectID { get; set; }
    [Required]
    [StringLength(50)]
    public string Title { get; set; }
    public string Description { get; set; }
    [DataType(DataType.Currency)]
    [Column(TypeName = "money")]
    public decimal EstimatedValue { get; set; }
    public int? InsuredID;
    public Insured? Insured;
    public int? InsuranceSubjectTypeID { get; set; }
    public InsuranceSubjectType? InsuranceSubjectType { get; set; }
    public ICollection<InsurancePolicy>? InsurancePolicies { get; set; }
}

I tried my code without the foreign key properties and only left the reference navigation property (deleted the int and left the object) and it worked okay, but I need FK ints for future implementation.

Here is GitHub repo link for more insight (branch model-extension): https://github.com/lenartgolob/Insurance-IS/tree/model-extension

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I added public virtual in front of my reference navigation properties and it solved the issue.

over 4 years ago · Santiago Trujillo Denunciar

0

For me the problem was the type of property ID in the FK part differed from the one in PK entity part:

    [Range(1, int.MaxValue, ErrorMessage = "Please provide valid payment id")]
    public int PaymentId { get; set; } // NOTICE INT HERE SHOULD BE LONG

    public virtual Payment Payment { get; set; }

where Payment had long property for ID:

public class Payment {
 public long Id {get;set;}
 //...
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda