简体   繁体   English

多次指定列名EF代码首次迁移

[英]Column name is specified more than once EF Code First Migration

I have two columns in a table which I created them using code first approach then I changed their data types in the C# model not the actual database and last I tried the later migration in order to change their data type in the database 我在表中有两列,我首先使用代码创建了它们,然后在C#模型中而不是在实际数据库中更改了它们的数据类型,最后我尝试了稍后的迁移以更改数据库中的数据类型

public partial class PriceAndMargin : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.Items", "Price", c => c.Int(nullable: false));
        AddColumn("dbo.Items", "ShortageMargin", c => c.Int(nullable: false));
    }

    public override void Down()
    {
        DropColumn("dbo.Items", "ShortageMargin");
        DropColumn("dbo.Items", "Price");
    }
}

If I got it write this code should drop the Price column and the ShortageMargin column and recreates them but instead it displays this message in the console for me 如果我编写了此代码,则应删除Price列和ShortageMargin列并重新创建它们,但相反,它将在控制台中显示此消息

Column names in each table must be unique. 每个表中的列名必须唯一。 Column name 'Price' in table 'dbo.Items' is specified more than once 表“ dbo.Items”中的列名“ Price”被多次指定

thanks in advance 提前致谢

If I got it write this code should drop the Price column and the ShortageMargin column and recreates them 如果我编写了此代码,则应删除Price列和ShortageMargin列并重新创建它们

Not really, your "up" adds the columns. 并非如此,您的“上”添加了各列。 The "down" section is only if you want to "downgrade" your database (ou revert the migration). 仅在您要“降级”数据库(还原迁移)时才使用“ down”部分。 I don't know how you changed the type of your object, and from which version you ran the 'add-migration' command but you should have a script with " AlterColumn instead of AddColumn 我不知道您如何更改对象的类型,以及从哪个版本运行了“ add-migration”命令,但是您应该使用“ AlterColumn而不是AddColumn ”脚本

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

相关问题 EF和TPT:在SET子句中多次指定列名 - EF and TPT : the column name is specified more than once in the SET clause Add-Migration一列名称被多次列出 - Add-Migration one column name is listed more than once 在 SET 子句中多次指定列名“phoneNumber” - The column name 'phoneNumber' is specified more than once in the SET clause 每个表中的列名必须唯一。 表“ HumanCustomers”中的列名“ LastName”已多次指定 - Column names in each table must be unique. Column name 'LastName' in table 'HumanCustomers' is specified more than once Linq Insert:列名称''在INSERT的SET子句或列列表中多次指定 - Linq Insert: The column name ' ' is specified more than once in the SET clause or column list of an INSERT EF代码优先迁移 - EF Code First migration EF6代码优先迁移中的架构名称重复 - Schema name duplication in EF6 code first migration 每个表中的列名必须唯一。 表“ UserLogins”中的列名“ department_Id”已多次指定 - Column names in each table must be unique. Column name 'department_Id' in table 'UserLogins' is specified more than once 每个表中的列名必须唯一。 表Entity1'中的列名'Id'被多次指定 - Column names in each table must be unique. Column name 'Id' in table Entity1' is specified more than once c#EF核心迁移代码第一个无效列 - c# EF Core Migration code First invalid column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM