Столбец, о котором идет речь. является PdfTemplateId в моей сущности.
public class Company : ISoftDelete
{
public int Id { get; set; }
[Required]
[StringLength(120)]
public string Name { get; set; }
[Display(Name = "Company Code")]
[Required]
[StringLength(80)]
public string CompanyCode { get; set; }
//[Display(Name = "Default PDF Template")]
//public int PdfTemplateId { get; set; }
//public PdfTemplate PdfTemplate { get; set; }
// Additional code...
}
public class CompanyConfiguration : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.HasMany(c => c.Facilities)
.WithOne(f => f.Company)
.OnDelete(DeleteBehavior.NoAction);
builder.HasMany(c => c.Products)
.WithOne(p => p.Company)
.OnDelete(DeleteBehavior.NoAction);
builder.HasMany(c => c.Customers)
.WithOne(c => c.Company)
.OnDelete(DeleteBehavior.NoAction);
}
}
Но свойство остается.
Когда я выполняю новую добавленную миграцию, никаких изменений не происходит. Ниже приведена часть моего ApplicationDbContextModelSnapshot.
modelBuilder.Entity("PegasusEntities.Models.Company", b =>
{
b.Property("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
b.Property("CompanyCode")
.IsRequired()
.HasMaxLength(80)
.HasColumnType("nvarchar(80)");
b.Property("DeletedOnUtc")
.HasColumnType("datetime2");
b.Property("IsDeleted")
.HasColumnType("bit");
b.Property("IsLimited")
.ValueGeneratedOnAdd()
.HasColumnType("bit")
.HasDefaultValue(false);
b.Property("Name")
.IsRequired()
.HasMaxLength(120)
.HasColumnType("nvarchar(120)");
b.Property("PdfTemplateId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("PdfTemplateId");
b.ToTable("Companies", (string)null);
});
Я попробовал раскомментировать удаленный столбец, добавить миграцию, затем снова удалить его и добавить еще одну миграцию.
К сожалению, я не получаю DropColumn во второй миграции. Вместо этого я получаю следующую миграцию.
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Companies_PdfTemplates_PdfTemplateId",
table: "Companies");
migrationBuilder.AlterColumn(
name: "PdfTemplateId",
table: "Companies",
type: "int",
nullable: true,
oldClrType: typeof(int),
oldType: "int");
migrationBuilder.AddForeignKey(
name: "FK_Companies_PdfTemplates_PdfTemplateId",
table: "Companies",
column: "PdfTemplateId",
principalTable: "PdfTemplates",
principalColumn: "Id");
}
К сожалению, я действительно не знаю, как устранить эту неполадку и восстановить синхронизацию.
Обновить
Я также попробовал добавить новый столбец с тем же именем и другим типом.public DateTime PdfTemplateId { get; set; }
Вот результат:
Build started...
Build succeeded.
Microsoft.EntityFrameworkCore.Model.Validation[10625]
The foreign key property 'Company.PdfTemplateId1' was created in shadow state because a conflicting property with the simple name 'PdfTemplateId' 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. See https://aka.ms/efcore-relationships for information on mapping relationships in EF Core.
Microsoft.EntityFrameworkCore.Model.Validation[10625]
The foreign key property 'Company.PdfTemplateId1' was created in shadow state because a conflicting property with the simple name 'PdfTemplateId' 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. See https://aka.ms/efcore-relationships for information on mapping relationships in EF Core.
А вот и миграция.
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Companies_PdfTemplates_PdfTemplateId",
table: "Companies");
migrationBuilder.DropIndex(
name: "IX_Companies_PdfTemplateId",
table: "Companies");
migrationBuilder.AlterColumn(
name: "PdfTemplateId",
table: "Companies",
type: "datetime2",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
oldClrType: typeof(int),
oldType: "int",
oldNullable: true);
migrationBuilder.AlterColumn(
name: "IsLimited",
table: "Companies",
type: "bit",
nullable: false,
oldClrType: typeof(bool),
oldType: "bit",
oldDefaultValue: false);
migrationBuilder.AddColumn(
name: "PdfTemplateId1",
table: "Companies",
type: "int",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_Companies_PdfTemplateId1",
table: "Companies",
column: "PdfTemplateId1");
migrationBuilder.AddForeignKey(
name: "FK_Companies_PdfTemplates_PdfTemplateId1",
table: "Companies",
column: "PdfTemplateId1",
principalTable: "PdfTemplates",
principalColumn: "Id");
}
Очевидно, что здесь все еще есть некоторые теневые свойства.
Обновление 2
Обратите внимание, что мой Таблица PdfTemplates имеет внешний ключ к моей таблице «Компании» (внешний ключ идет в другую сторону). Поэтому я задавался вопросом, может ли это вызвать путаницу. Поэтому я удалил это и создал новую миграцию. В данном случае ненужные свойства были удалены, но затем создана совершенно новая таблица, которой нет в моем коде CompanyPdfTemplate. Но я не могу хоть убей понять, что является причиной этого.protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Companies_PdfTemplates_PdfTemplateId",
table: "Companies");
migrationBuilder.DropForeignKey(
name: "FK_PdfTemplates_Companies_CompanyId",
table: "PdfTemplates");
migrationBuilder.DropIndex(
name: "IX_PdfTemplates_CompanyId",
table: "PdfTemplates");
migrationBuilder.DropIndex(
name: "IX_Companies_PdfTemplateId",
table: "Companies");
migrationBuilder.DropColumn(
name: "CompanyId",
table: "PdfTemplates");
migrationBuilder.DropColumn(
name: "PdfTemplateId",
table: "Companies");
migrationBuilder.AlterColumn(
name: "IsLimited",
table: "Companies",
type: "bit",
nullable: false,
oldClrType: typeof(bool),
oldType: "bit",
oldDefaultValue: false);
migrationBuilder.CreateTable(
name: "CompanyPdfTemplate",
columns: table => new
{
CompaniesId = table.Column(type: "int", nullable: false),
PdfTemplatesId = table.Column(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_CompanyPdfTemplate", x => new { x.CompaniesId, x.PdfTemplatesId });
table.ForeignKey(
name: "FK_CompanyPdfTemplate_Companies_CompaniesId",
column: x => x.CompaniesId,
principalTable: "Companies",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_CompanyPdfTemplate_PdfTemplates_PdfTemplatesId",
column: x => x.PdfTemplatesId,
principalTable: "PdfTemplates",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_CompanyPdfTemplate_PdfTemplatesId",
table: "CompanyPdfTemplate",
column: "PdfTemplatesId");
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... ove-column
Мобильная версия