'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
Solution 1:[1]
I added public virtual
in front of my reference navigation properties and it solved the issue.
Solution 2:[2]
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;}
//...
}
Solution 3:[3]
For me was that I mistakenly referred the same entity twice:
modelBuilder.Entity<ProductCategoryProduct>()
.HasKey(tl => new {tl.ProductCategoryId, tl.ProductId});
modelBuilder.Entity<ProductCategoryProduct>()
.HasOne(t => t.ProductCategory)
.WithMany(t => t.ProductCategoryProducts)
.HasForeignKey(t => t.ProductCategoryId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<ProductCategoryProduct>()
.HasOne(p => p.ProductCategory) // this should be Product
.WithMany(p => p.ProductCategoryProducts)
.HasForeignKey(p => p.ProductId)
.OnDelete(DeleteBehavior.Restrict);
Solution 4:[4]
Ok.. For me.
I had Parent/Child relationship.
And I had put in the scalar's for the parent FK, and had defined the orm mapping for the scalar FK "int".
public class MyParent
{
public int MyParentKey { get; set; }
public ICollection<MyChild> MyKids { get; set; }
}
public class MyChild
{
public int MyChildKey { get; set; }
public int MyParentKey { get; set; } /* FK Scalar */
public MyParent TheMyParent { get; set; }
}
I had Orm-Mapped the MyParentKey on the MyChildOrmMap.
But I had NOT orm-mapped the "TheMyParent".
Once I coded the 1:N relationship for TheMyParent and the reciprocal MyKids, the error went away.
Aka, I was missing the below (the below code would be in the MyChildMap (the fluent orm map)
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
builder.HasOne<MyParent>(me => me.TheMyParent)
.WithMany(parent => parent.MyKids)
.HasForeignKey(me => me.MyParentKey)
.HasConstraintName("FK_MyChild_To_MyParent_MyParentKey");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Edi |
Solution 3 | Edi |
Solution 4 | granadaCoder |