[英]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.
Product.cs
. 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的表中。
ProductUnitPrice
to Products table. 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.
[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.