简体   繁体   English

每个表中的列名必须是唯一的。 表 'dbo.Foos' 中的列名 'StripeRecipientId' 被指定多次

[英]Column names in each table must be unique. Column name 'StripeRecipientId' in table 'dbo.Foos' is specified more than once

I have a model class named Foo that has, among others, these properties.我有一个名为Foo的模型类,其中包含这些属性。

public string StripeRecipientId { get; set; }

public override bool HasProvidedBillingInformation
{
    get
    {
        // return !string.IsNullOrEmpty(this.StripeRecipientId);

        return false;
    }
}

I have enabled migrations and am using Code First.我已启用迁移并使用 Code First。 When I run the update-database commandlet, whether with -Force option is specified or not, I get this error:当我运行update-database commandlet 时,无论是否指定了-Force选项,我都会收到此错误:

Column names in each table must be unique. Column name 'StripeRecipientId' in table 'dbo.Foos' is specified more than once.

I double-checked and triple checked and there's only one column of that name in my model as well as in the table.我反复检查和三次检查,在我的模型和表格中只有一列该名称。 This column was created already by a previous run of the update-database commandlet just a while ago.此列已由不久前上一次运行update-database commandlet 创建。

I am tempted to delete my database and then apply the migrations, but that will mean me having to create a lot of test data just to be able to test the feature I am working on just now.我很想删除我的数据库,然后应用迁移,但这意味着我必须创建大量测试数据才能测试我刚刚正在处理的功能。

I am using Entity Framework v6.1.2.我正在使用实体框架 v6.1.2。

How do I get rid of this error?我如何摆脱这个错误?

Run the Add-Migration command with the -IgnoreChanges flag.使用-IgnoreChanges标志运行Add-Migration命令。 Then run Update-Database again.然后再次运行Update-Database

-Update- -更新-

These commands should be run in the Package Manager Console.这些命令应该在包管理器控制台中运行。 From the main menu: Tools-> NuGet Package Manager -> Package Manager Console.从主菜单:工具-> NuGet 包管理器 -> 包管理器控制台。

Add-Migration -IgnoreChanges因为它对我有用,但仍然抛出了更多错误。

If you have also added these properties in the Database tables you will continue to get these errors.如果您还在数据库表中添加了这些属性,您将继续收到这些错误。 So I had to delete the properties from my own table to achieve a re-scaffolding and updated the database subsequently.所以我不得不从我自己的表中删除属性以实现重新脚手架并随后更新数据库。 So run Add-Migration after you must have removed the properties and you will obtain a satisfactory outcome, then run Update-Database it is also expected to be satisfactory.因此,在您必须删除属性后运行Add-Migration并且您将获得令人满意的结果,然后运行Update-Database也预期会令人满意。

This worked for me.这对我有用。

  1. I run this Add-Migration -IgnoreChanges and i got error that your previous two migrations are in pending.我运行此Add-Migration -IgnoreChanges并且我收到错误消息,您的前两次迁移处于挂起状态。 Example: Migration-1 , Migration-2示例: Migration-1 , Migration-2

  2. I run update-database -target Migration-1 and我运行update-database -target Migration-1

    update-database -target Migration-2

  3. Database Successfully created.数据库创建成功。 Running Seed method.

This works for me in two steps:这分两步对我有用:

Step 1) Just delete this column from a table in Database
Step 2) Rebuild the Project and run Project.

Simple delete files in Migration folder and run following command:简单删除 Migration 文件夹中的文件并运行以下命令:

add-migration seednew1

And then:进而:

update-database -verbose

对我来说,在添加/删除操作期间我必须搞砸的数据库快照与实际保存在数据库中的不同,所以我最终放弃了快照更改并重新应用迁移。

the update-database process is referencing the older migrations [which perhaps contain some add columns scripts] ... DELETE the older migrations with these adds... remember to KEEP the SNAPSHOT [summary of the saved scripts] ... and run your migration again... it should work!更新数据库进程正在引用较旧的迁移 [其中可能包含一些添加列脚本] ...使用这些添加删除较旧的迁移...记得保留快照 [已保存脚本的摘要] ...并运行您的再次迁移......它应该可以工作! all the best :))祝一切顺利 :))

Regarding People's issues with getting -IgnoreChanges to work关于人们让 -IgnoreChanges 工作的问题

So for my case what I found the issue to be was that the -IgnoreChanges option is specific to Entity Framework, while I was using EFCore.因此,就我而言,我发现问题是 -IgnoreChanges 选项特定于实体框架,而我使用的是 EFCore。 What I found to be a solution was reviewing the individual commits in the migrations folder of your database project.我发现的解决方案是查看数据库项目的迁移文件夹中的各个提交。 By following the advice of this link and commenting out the specific Up function body I was able to run Update-Database .通过遵循此链接的建议并注释掉特定的 Up 函数体,我能够运行Update-Database Updating the database works.更新数据库工作。

I had the following case: I first added a column to the database using a query, then I tried to do "update-database" in package manager console.我有以下情况:我首先使用查询向数据库添加了一列,然后我尝试在包管理器控制台中执行“更新数据库”。 He gave me an error.他给了我一个错误。 I deleted from the table the column that was in error and did "update-database" in package manager console vs2019 again and it worked我从表中删除了出错的列,并再次在包管理器控制台 vs2019 中执行了“更新数据库”,并且它起作用了

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

相关问题 每个表中的列名必须唯一。 表“ UserLogins”中的列名“ department_Id”已多次指定 - Column names in each table must be unique. Column name 'department_Id' in table 'UserLogins' is specified more than once 实体框架5 CodeFirst-“每个表中的列名必须唯一。” - Entity Framework 5 CodeFirst - “Column names in each table must be unique.” 错误:每个表中的列名必须是唯一的 - Error: Column names in each table must be unique 实体框架:迁移每个表中的列名必须是唯一的 - Entity Framework: migration Column names in each table must be unique Entity Framework Core 错误“每个表中的列名必须是唯一的” - Entity Framework Core error "Column names in each table must be unique" EF核心标识外键引发每个表中的列名必须唯一 - EF Core Identity Foreign Key throws Column names in each table must be unique EF和TPT:在SET子句中多次指定列名 - EF and TPT : the column name is specified more than once in the SET clause 多次指定列名EF代码首次迁移 - Column name is specified more than once EF Code First Migration EF5代码优先迁移:使用RenameColumn后,“每个表中的列名必须是唯一的”错误 - EF5 Code First Migrations: “Column names in each table must be unique” error after using RenameColumn 模式中的每个类型名称都必须是唯一的。 代码优先 - Each type name in a schema must be unique. Code first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM