简体   繁体   English

EF Core 导航到父类问题的 2 个属性

[英]EF Core Navigating to 2 Properties of the Parent Class Issue

I am running a migration on a new Class I created that has 2 objects within it that reference different objects of the same type.我正在我创建的一个新类上运行迁移,其中有 2 个对象引用相同类型的不同对象。 Here is the class这是课堂

public class AccountOpenerWorkflowStep 
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }
        public string Controller { get; set; }
        public string Action { get; set; }
        public bool DefaultStep { get; set; }

        [Column("TrueWorkflowStepId")]
        public virtual AccountOpenerWorkflowStep TrueWorkflowStep { get; set; }

        [Column("FalseWorkflowStepId")]
        public virtual AccountOpenerWorkflowStep FalseWorkflowStep { get; set; }
    }

So my two properties TrueWorkflowStep and FalseWorkflowStep will point to other objects in the same table.所以我的两个属性TrueWorkflowStepFalseWorkflowStep将指向同一个表中的其他对象。

The issue is when I run this migration it only creates the column FalseWorkflowStep and not the True one.问题是当我运行此迁移时,它只创建列FalseWorkflowStep而不是 True 列。

Here is an except from the Migration这是迁移中的一个例外

migrationBuilder.CreateTable(
            name: "AccountOpenerWorkflowSteps",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn),
                CreatedDate = table.Column<DateTime>(nullable: false),
                ModifiedDate = table.Column<DateTime>(nullable: false),
                Controller = table.Column<string>(nullable: true),
                Action = table.Column<string>(nullable: true),
                DefaultStep = table.Column<bool>(nullable: false),
                FalseWorkflowStepId = table.Column<int>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AccountOpenerWorkflowSteps", x => x.Id);
                table.ForeignKey(
                    name: "FK_AccountOpenerWorkflowSteps_AccountOpenerWorkflowSteps_False~",
                    column: x => x.FalseWorkflowStepId,
                    principalTable: "AccountOpenerWorkflowSteps",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.SetNull);
            });

As you can see it is only inserting the one column of the same type.如您所见,它仅插入了同一类型的一列。

Can anyone tell me why it does not recognize both of these columns?谁能告诉我为什么它不能识别这两列?

The [Column] attribute isn't appropriate here, because it should be applied to primitive properties, not references. [Column]属性在这里不合适,因为它应该应用于原始属性,而不是引用。 EF just ignores the attributes (using different column names confirms this), falls back on its default mapping conventions --and then fails. EF只是忽略属性(使用不同的列名来确认这一点),退回其默认映射约定-然后失败。

I happened to stumble upon a similar problem yesterday. 昨天我偶然发现了一个类似的问题 EF's default conventions seem to have a hard time handling two uniform relationships in one class. EF的默认约定似乎很难在一个类中处理两个统一的关系。

I didn't try your model in Npgsql, but with Sql Server EF fails likewise. 我没有在Npgsql中尝试您的模型,但是使用Sql Server EF同样失败。 The solution is to use the correct attribute: 解决方案是使用正确的属性:

[ForeignKey("TrueWorkflowStepId")]
public virtual AccountOpenerWorkflowStep TrueWorkflowStep { get; set; }

[ForeignKey("FalseWorkflowStepId")]
public virtual AccountOpenerWorkflowStep FalseWorkflowStep { get; set; }

Or fluent mapping: 或流利的映射:

modelBuilder.Entity<AccountOpenerWorkflowStep>().HasOne(x => x.TrueWorkflowStep).WithMany()
    .HasForeignKey("TrueWorkflowStepId").IsRequired(false);
modelBuilder.Entity<AccountOpenerWorkflowStep>().HasOne(x => x.FalseWorkflowStep).WithMany()
    .HasForeignKey("FalseWorkflowStepId").IsRequired(false);

Here is the explain 是解释

EF Core only supports having one mapping in the model for each entity type. EF Core仅支持每种实体类型在模型中具有一个映射。 Ie there aren't any supported cases in which two instance of the same type could end up being stored in different tables or different columns. 即,没有任何支持的情况,其中相同类型的两个实例最终可能存储在不同的表或不同的列中。

If you want something like this there are a couple of alternatives you can try. 如果您想要这样的东西,可以尝试几种方法。 The most appropriate one is going to depend on your scenario: 最合适的一种取决于您的方案:

Having two DbSets of two different types. 具有两个不同类型的两个DbSet。 The two types can inherit from (and even have all their properties defined in) a common base type as long as that base type isn't mapped in the EF Core model. 两种类型都可以从通用基本类型继承(甚至在其所有属性中定义),只要该基本类型未在EF Core模型中进行映射即可。

Having two separate derived DbContext types which map the Venda type to different tables. 具有两个单独的派生DbContext类型,这些类型将Venda类型映射到不同的表。

So I would recommend you try to add different class that inherit same property from base class 因此,我建议您尝试添加从基类继承相同属性的其他类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM