-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicate Foreign Key Constraints created in Migrations with TPH inheritance #5769
Comments
Possibly related to #5665 |
Validate that matching foreign keys and indexes are compatible Fixes #5769
Validate that matching foreign keys and indexes are compatible Fixes #5769
Validate that matching foreign keys and indexes are compatible Fixes #5769
@AndriySvyryd Thanks for figuring this out! |
I am facing another similar but different problem @rowanmiller @AndriySvyryd Steps to reproduce public abstract class PersonBaseViewModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string IdNumber { get; set; }
public virtual CityViewModel CityVM { get; set; }
}
public abstract class ServicePersonViewModel: PersonBaseViewModel
{
public string PersonnelNumber { get; set; }
}
public class CitizenViewModel : PersonBaseViewModel
{
//...
}
public class PoliceViewModel : ServicePersonViewModel
{
//...
}
public class CityViewModel
{
public int Id { get; set; }
public ICollection<Citizen> People { get; set; }
public ICollection<Police> Securities { get; set; }
} The migrations being generated include codes similar to these: modelBuilder.Entity("PersonBaseViewModel", b =>
{
//...
b.Property<int?>("CityVMId");
//...
b.HasIndex("CityVMId");
//...
});
modelBuilder.Entity("CitizenViewModel", b =>
{
b.HasBaseType("PersonBaseViewModel");
//...
b.Property<int?>("CityViewModelId");
//...
b.HasIndex("CityViewModelId");
//...
});
modelBuilder.Entity("PoliceViewModel", b =>
{
b.HasBaseType("ServicePersonViewModel");
//...
b.Property<int?>("CityViewModelId");
//...
b.HasIndex("CityViewModelId");
//...
});
//--------------------------------------------------
modelBuilder.Entity("PersonBaseViewModel", b =>
{
b.HasOne("CityViewModel", "CityVM")
.WithMany()
.HasForeignKey("CityVMId");
});
modelBuilder.Entity("CitizenViewModel", b =>
{
b.HasOne("CityViewModel", "CityVM")
.WithMany()
.HasForeignKey("CityViewModelId");
});
modelBuilder.Entity("PoliceViewModel", b =>
{
b.HasOne("CityViewModel", "CityVM")
.WithMany()
.HasForeignKey("CityViewModelId");
});
//--------------------------------------------------
migrationBuilder.CreateTable(
name: "PersonBases",
columns: table => new
{
//...
CityVMId = table.Column<int>(nullable: true),
//...
CityViewModelId = table.Column<int>(nullable: true),
//...
},
constraints: table =>
{
table.PrimaryKey("PK_PersonBases", x => x.Id);
table.ForeignKey(
name: "FK_PersonBases_Cities_CityVMId",
column: x => x.CityVMId,
principalTable: "Cities",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_PersonBases_Cities_CityViewModelId",
column: x => x.CityViewModelId,
principalTable: "Cities",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}); The issue Removing the extra foreign key from migrations fixes the database creation, however when inserting data into the table using EF Core, an exception with message 'Column CityVMId could not be found' (Or something similar) is being raised. Therefore, an empty column for CityVMId must be existing in the database for the EF Core to work when inserting/updating data. Concerns Further technical details |
@Sojaner The issue you are seeing is not related, could you open a new issue with this info? modelBuilder.Entity<CitizenViewModel>().HasOne(c => c.CityVM).WithMany(c => c.People);
modelBuilder.Entity<PoliceViewModel>() .HasOne(p => p.CityVM).WithMany(c => c.Securities); |
Thanks for the workaround @AndriySvyryd |
Steps to reproduce
The issue
Within the generated migration:
Further technical details
EF Core version: 1.0.0-rc2-final
Operating system: Win 7 x64
Visual Studio version: VS 2015 Update 2
The text was updated successfully, but these errors were encountered: