简体   繁体   English

实体框架:迁移每个表中的列名必须是唯一的

[英]Entity Framework: migration Column names in each table must be unique

Now I have a problem with my source code. 现在我的源代码有问题。 I don't know reason. 我不知道理由。 I am using EntityFramework.6.1.3. 我正在使用EntityFramework.6.1.3.

  1. Now, I have a Model folder, and in this folder I have Product.cs . 现在,我有一个Model文件夹,在这个文件夹中我有Product.cs Here is content of Product.cs file. 这是Product.cs文件的内容。

` `

[Data]
[Column(TypeName = "numeric"), Display(Name = "ProductUnitPrice")]
public decimal ProductUnitPrice { get; set; }

I have a table Products is exits. 我有一张桌子Products是退出的。 I still have not file Migration to add new ProductUnitPrice to Products table. 我仍然没有文件迁移将新的ProductUnitPrice添加到Products表。

When I run my project, it is automatic insert new ProductUnitPrice column to Product's table. 当我运行我的项目时,它会自动将新的ProductUnitPrice列插入到Product的表中。

  1. However, I want to create a file Migration to add new ProductUnitPrice to Products table. 但是,我想创建一个文件Migration以将新的ProductUnitPrice添加到Products表。

Here is file migration. 这是文件迁移。

` `

public partial class AddColumnToProducts : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.Products", "ProductUnitPrice", c => c.Decimal(nullable: false, precision: 18, scale: 2, storeType: "numeric"));
        }
        public override void Down()
        {
            DropColumn("dbo.Products", "ProductUnitPrice");
        }
    }

` `

But when I run my application I receive a errors message Column names in each table must be unique. Column name 'ProductUnitPrice' in table 'dbo.Products' is specified more than once. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Column names in each table must be unique. Column name 'ProductUnitPrice' in table 'dbo.Products' is specified more than once. 但是当我运行我的应用程序时,我收到一条错误消息Column names in each table must be unique. Column name 'ProductUnitPrice' in table 'dbo.Products' is specified more than once. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Column names in each table must be unique. Column name 'ProductUnitPrice' in table 'dbo.Products' is specified more than once. Column names in each table must be unique. Column name 'ProductUnitPrice' in table 'dbo.Products' is specified more than once. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Column names in each table must be unique. Column name 'ProductUnitPrice' in table 'dbo.Products' is specified more than once.

  1. To resolve above problem, I added [NotMapped] to Model file 为解决上述问题,我将[NotMapped]添加到模型文件中
    [Data]
    [Column(TypeName = "numeric"), Display(Name = "ProductUnitPrice")]
    [NotMapped]
    public decimal ProductUnitPrice { get; set; }

I can run application and ProductUnitPrice is insert to DB. 我可以运行应用程序,ProductUnitPrice是插入到DB。 However, my problem is I can update data to ProductUnitPrice column. 但是,我的问题是我可以将数据更新到ProductUnitPrice列。 If I remove migration files and [NotMapped], I can update data to ProductUnitPrice column. 如果我删除迁移文件和[NotMapped],我可以将数据更新到ProductUnitPrice列。

What is problem? 有什么问题?

I want to have a Migration file to add ProductUnitPrice column and I want I can update data to this column. 我想有一个迁移文件来添加ProductUnitPrice列,我希望我可以将数据更新到此列。

Please help me solve it. 请帮我解决一下。

This means you already have this column in your table. 这意味着您已经在表中拥有此列。

What comes to my mind is that you have set something like this in your migration's configuration file: 我想到的是你在迁移的配置文件中设置了这样的东西:

AutomaticMigrationsEnabled = true;

That means everytime you change something in your model, migrations are "updated" automatically. 这意味着每次更改模型中的某些内容时,迁移都会自动“更新”。

If so, try setting AutomaticMigrationsEnabled to false, delete migration, run project, then try adding migration again. 如果是这样,请尝试将AutomaticMigrationsEnabled设置为false,删除迁移,运行项目,然后再次尝试添加迁移。

暂无
暂无

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

相关问题 Entity Framework Core 错误“每个表中的列名必须是唯一的” - Entity Framework Core error "Column names in each table must be unique" 错误:每个表中的列名必须是唯一的 - Error: Column names in each table must be unique 每个表中的列名必须是唯一的例外 - Column names in each table must be unique exception 每个表中的列名必须唯一。 表Entity1'中的列名'Id'被多次指定 - Column names in each table must be unique. Column name 'Id' in table Entity1' is specified more than once 每个表中的列名必须唯一。 表“ HumanCustomers”中的列名“ LastName”已多次指定 - Column names in each table must be unique. Column name 'LastName' in table 'HumanCustomers' is specified more than once EF核心标识外键引发每个表中的列名必须唯一 - EF Core Identity Foreign Key throws Column names in each table must be unique 实体框架验证错误 - 类型中的每个属性名称必须是唯一的 - Entity Framework validation error - Each property name in a type must be unique 每个表中的列名必须唯一。 表“ UserLogins”中的列名“ department_Id”已多次指定 - Column names in each table must be unique. Column name 'department_Id' in table 'UserLogins' is specified more than once 代码优先迁移 - 实体框架 - 无法向表中添加列 - Code First Migration - Entity Framework - unable to add column to the table 实体框架迁移中的填充表 - Fill table in Entity Framework Migration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM