I am having some foreign key problems, it turns out that I am performing a revision to an old system in which a new table is added, and when doing so, the following comes up.
Summing up, I have the following 3 classes.
public class StepUp
{
[Key]
[Column(Order = 0)]
public string CodigoDelfos { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Revision { get; set; }
public virtual ICollection<Accesorio_x_Trafos> Accesorios { get; set; }
//Muchas otras propiedades..
}
public class Transformador
{
[Key]
[Column(Order = 0)]
public string Codigo_delfos { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Revision { get; set; }
public virtual ICollection<Accesorio_x_Trafos> Accesorio_x_Trafos { get; set; }
//Muchas otras propiedades..
}
public class Accesorio_x_Trafos
{
[Key]
[Column(Order = 0)]
public string CodigoAccesorio { get; set; }
[Key]
[Column(Order = 1)]
public string CodigoDelfos { get; set; }
[Key]
[Column(Order = 2)]
public int Revision { get; set; }
public int? Cantidad { get; set; }
}
The thing is, as long StepUp
as Transformador
they have a 1-* relationship with Accesorio_x_Trafos
.
When migrating, the database is created successfully (FKs are added)
(Ignore the FK Accessories which is one of the properties that I removed to not have so much code)
The issue is that when I try to both modify an object of type Transformador
(I do it as follows)
//Obtengo un trafo cualquiera
var transformador = mContext.Transformador.First();
//Cargo datos de ejemplo:
transformador.Accesorio_x_Trafos = new List<Accesorio_x_Trafos>
{
new Accesorio_x_Trafos
{ CodigoDelfos = transformador.Codigo_delfos, Revision = transformador.Revision, Cantidad = 1, CodigoAccesorio = "05-CBPA0150000802000P0800"},
new Accesorio_x_Trafos
{ CodigoDelfos = transformador.Codigo_delfos, Revision = transformador.Revision, Cantidad = 2, CodigoAccesorio = "05-CBPA0150000803150P0700"},
new Accesorio_x_Trafos
{ CodigoDelfos = transformador.Codigo_delfos, Revision = transformador.Revision, Cantidad = 3, CodigoAccesorio = "05-CBPA0150000803150P0700"}
};
//Guardo
mContext.SaveChanges();
Which triggers the following exception:
An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while...
And seeing the InnerException, it references a FK issue
INSERT statement in conflict with the FOREIGN KEY constraint \"FK_dbo.Accesorio_x_Trafos_dbo.StepUps_CodigoDelfos_Revision\". The conflict has appeared in the database \"DBTrafos\", table \"dbo.StepUps\".\r\nThe statement has been terminated.
How can I solve my problem?
Or if it is the case, how can I get more detail of what specifically is the conflict with the foreign key?
Thank you!!
I think your error is in the mapping of the class to the database. The property that acts as a foreign key must be specified with the Entity Framework tag
[ForeignKey(name)]
, being as follows:I really don't know if it is possible that it is a foreign key of two different attributes from different tables. Although it could be solved by making a generalization of the Transformer and StepUp tables. Or since both have the same fields of the same type, leave only one table adding a field that specifies the type (Transformer or StepUp). I leave you a link where you can see the use of the ForeignKey tag
Reviewing your case a bit more, I see that the error you mention is mentioned on the Microsoft Link page in the section: Add a migration.
I hope this helps you.
Generalizing the tables and adding a type to differentiate them, it would look like this:
I leave you another link with an explanation and simple examples of the generalization in the entity-relationship model link
I ended up fixing it with a little help from Alexmaister's comments.
I did this by creating an abstract class that overrides
Accesorio_x_Trafos
in such a way that I then specify each inherited class with its corresponding foreign key and navigation property.In this way, in each of the corresponding classes, I used properties, either of type
ICollection<AccesorioStepUp>
such asICollection<AccesorioDistribucion>
.