简体   繁体   中英

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.

  1. Now, I have a Model folder, and in this folder I have Product.cs . Here is content of Product.cs file.

`

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

I have a table Products is exits. I still have not file Migration to add new ProductUnitPrice to Products table.

When I run my project, it is automatic insert new ProductUnitPrice column to Product's table.

  1. However, I want to create a file Migration to add new ProductUnitPrice to Products table.

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.

  1. To resolve above problem, I added [NotMapped] to Model file
    [Data]
    [Column(TypeName = "numeric"), Display(Name = "ProductUnitPrice")]
    [NotMapped]
    public decimal ProductUnitPrice { get; set; }

I can run application and ProductUnitPrice is insert to DB. However, my problem is I can update data to ProductUnitPrice column. If I remove migration files and [NotMapped], I can update data to ProductUnitPrice column.

What is problem?

I want to have a Migration file to add ProductUnitPrice column and I want I can update data to this column.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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