我正在使用实体框架。该表已经存在,且列my_column。我将属性my_column添加到类中以访问该列。然后,该程序似乎尝试创建该列,但是由于该列已存在而失败。

Column names in each table must be unique. Column name 'my_column' in table 'my_table' is specified more than once.


有没有一种方法可以避免尝试创建该列并认识到现有列就是它应该使用的列?

最佳答案

您可以在更新数据库之前编辑迁移。删除重复项,看看会发生什么。

            migrationBuilder.CreateTable(

            name: "InvoiceConsumers",

            columns: table => new

            {

               …,

                InvoiceId = table.Column<int>(nullable: false),

                InvoiceId1 = table.Column<int>(nullable: true)

            },

            constraints: table =>

            {

                table.PrimaryKey("PK_InvoiceConsumers", x => x.Id);

                table.ForeignKey(

                    name: "FK_InvoiceConsumers_Consumers_ConsumerId",

                    …);

                table.ForeignKey(

                    name: "FK_InvoiceConsumers_Invoices_InvoiceId",

                    …

                    onDelete: ReferentialAction.Cascade);

                table.ForeignKey(

                    …

                    column: x => x.InvoiceId1,

                    principalTable: "Invoices",

                    …

                    );

            });

关于c# - Entity Framework 尝试创建重复的列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24685789/

10-10 07:42