[英]Error: Column names in each table must be unique
I am getting an error upon executing update-database which states: "Column names in each table must be unique. Column name 'OrderRecieveDate' in table 'dbo.OrderForms' is specified more than once."执行 update-database 时出现错误,其中指出:“每个表中的列名必须是唯一的。表 'dbo.OrderForms' 中的列名 'OrderRecieveDate' 被指定了多次。” I've tried everything I can think of to fix it and nothing has panned out thus far.
我已经尝试了我能想到的一切来修复它,但到目前为止还没有任何结果。 Previously I was able to add/update/delete rows from my table through the website i've created but now that feature does not even seem to be working.
以前我可以通过我创建的网站从我的表中添加/更新/删除行,但现在该功能似乎甚至不起作用。 I can't even get the database to add my seed data at this point.
此时我什至无法让数据库添加我的种子数据。
My Context/Models file:我的上下文/模型文件:
public class OrderFormContext : DbContext
{
public DbSet<OrderForm> OrderForms { get; set; }
}
public class OrderForm
{
[Key, Display(Name= "Order ID" )]
[ScaffoldColumn(false)]
public int OrderID { get; set; }
private DateTime _date = DateTime.Today;
[Display(Name = "Date Posted")]
[ScaffoldColumn(false)]
public DateTime OrderPostDate
{
get { return _date; }
set { _date = value; }
}
[EnumDataType(typeof(PersonReqID)),Display(Name = "Person Requesting")]
public PersonReqID PersonReq { get; set; }
[StringLength(100), Display(Name = "Grant")]
public string GrantID { get; set; }
[StringLength(10000), Display(Name = "Product Description"), DataType(DataType.MultilineText)]
public string ItemDescription { get; set; }
[StringLength(100), Display(Name = "Quantity")]
public string ItemQuantity { get; set; }
[StringLength(100), Display(Name = "Price")]
public string UnitPrice { get; set; }
[StringLength(100), Display(Name = "Company")]
public string CompanyID { get; set; }
[StringLength(100), Display(Name = "Catalog #")]
public string CatalogNum { get; set; }
[ScaffoldColumn(false)]
[Range(typeof(DateTime), "1/1/1111", "1/1/3000", ErrorMessage = "Please provide a date.")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime OrderMadeDate { get; set; }
[ScaffoldColumn(false)]
[Range(typeof(DateTime), "1/1/1111", "1/1/3000", ErrorMessage = "Please provide a date.")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime OrderRecieveDate { get; set; }
[ScaffoldColumn(false)]
[EnumDataType(typeof(PersonRecID)), Display(Name = "Person Recieving")]
public PersonRecID PersonRec { get; set; }
[ScaffoldColumn(false)]
[StringLength(100), Display(Name = "Item Location")]
public string ItemLoc { get; set; }
}
My Configuration.cs file:我的 Configuration.cs 文件:
context.OrderForms.AddOrUpdate(
new OrderForm
{
OrderID=1,
OrderPostDate=DateTime.Now,
PersonReq=PersonReqID.Jake,
GrantID="NASA",
ItemDescription="Grenades",
ItemQuantity="100",
UnitPrice="1.50",
CompanyID="Grenades UNLMTD",
CatalogNum="G1001",
PersonRec=PersonRecID.Ajit,
ItemLoc="Freezer"
}
);
context.SaveChanges();
and finally my xxxxxxx_initial.cs migration file:最后是我的 xxxxxxx_initial.cs 迁移文件:
public override void Up()
{
AddColumn("dbo.OrderForms", "OrderRecieveDate", c =>
c.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));
AddColumn("dbo.OrderForms", "OrderMadeDate", d =>
d.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));
}
public override void Down()
{
DropColumn("dbo.OrderForms", "OrderRecieveDate");
DropColumn("dbo.OrderForms", "OrderMadeDate");
}
I'm very new to web design and so i'm just concluding everything that I think could potentially cause an issue.我对网页设计非常陌生,所以我只是总结了我认为可能导致问题的所有内容。 Here is the table definition for my OrderForms.dbo file.
这是我的 OrderForms.dbo 文件的表定义。
CREATE TABLE [dbo].[OrderForms] (
[OrderID] INT IDENTITY (1, 1) NOT NULL,
[GrantID] NVARCHAR (100) NULL,
[ItemDescription] NVARCHAR (MAX) NULL,
[ItemQuantity] NVARCHAR (100) NULL,
[UnitPrice] NVARCHAR (100) NULL,
[CompanyID] NVARCHAR (100) NULL,
[CatalogNum] NVARCHAR (100) NULL,
[OrderDate] NVARCHAR (100) NULL,
[ItemLoc] NVARCHAR (100) NULL,
[PersonReq] INT DEFAULT ((0)) NULL,
[PersonRec] INT DEFAULT ((0)) NULL,
[OrderPostDate] DATETIME DEFAULT ('1900-01-01T00:00:00.000') NULL,
[OrderRecieveDate] DATETIME DEFAULT ('2013-01-01T00:00:00.000') NULL,
[OrderMadeDate] DATETIME DEFAULT ('2013-01-01T00:00:00.000') NULL,
CONSTRAINT [PK_dbo.OrderForms] PRIMARY KEY CLUSTERED ([OrderID] ASC)
); );
Any help would be greatly appreciated.任何帮助将不胜感激。 I can't seem to figure out how the column is being created twice.
我似乎无法弄清楚该列是如何被创建两次的。 I have tried deleting the database and previous migrations and doing 'enable-migrations -forced' and restarting from there to no avail.
我尝试删除数据库和以前的迁移并执行“启用迁移 - 强制”并从那里重新启动无济于事。 This is probably something that has to do with a lack of background knowledge on EF from my end.
这可能与我缺乏关于 EF 的背景知识有关。 Thanks in advance for any help!
提前感谢您的帮助!
So I may be misinterpreting what you have, but dont you create the columns once in the definition:所以我可能会误解你所拥有的,但你不要在定义中创建一次列:
CREATE TABLE [dbo].[OrderForms] (
[OrderID] INT IDENTITY (1, 1) NOT NULL,
[GrantID] NVARCHAR (100) NULL,
[ItemDescription] NVARCHAR (MAX) NULL,
[ItemQuantity] NVARCHAR (100) NULL,
[UnitPrice] NVARCHAR (100) NULL,
[CompanyID] NVARCHAR (100) NULL,
[CatalogNum] NVARCHAR (100) NULL,
[OrderDate] NVARCHAR (100) NULL,
[ItemLoc] NVARCHAR (100) NULL,
[PersonReq] INT DEFAULT ((0)) NULL,
[PersonRec] INT DEFAULT ((0)) NULL,
[OrderPostDate] DATETIME DEFAULT ('1900-01-01T00:00:00.000') NULL,
[OrderRecieveDate] DATETIME DEFAULT ('2013-01-01T00:00:00.000') NULL,
[OrderMadeDate] DATETIME DEFAULT ('2013-01-01T00:00:00.000') NULL,
CONSTRAINT [PK_dbo.OrderForms] PRIMARY KEY CLUSTERED ([OrderID] ASC)
Then attempt to create them again然后尝试再次创建它们
public override void Up()
{
AddColumn("dbo.OrderForms", "OrderRecieveDate", c =>
c.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));
AddColumn("dbo.OrderForms", "OrderMadeDate", d =>
d.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));
}
If you need some troubleshooting advice you can use the sys.tables and sys.columns to find the values out, and an IF NOT EXISTS on your add columns.如果您需要一些故障排除建议,您可以使用 sys.tables 和 sys.columns 找出值,并在添加列上使用 IF NOT EXISTS。
This will require some modification but it might be enough to get you going.这将需要一些修改,但它可能足以让你去。
string columnToAdd = " ADD [" + columnList[i] + "] " + columnDataTypeList[i];
string alterTableSQL = @"ALTER TABLE " + DestinationTbl + columnList[i];
string columnMakerSQL += @"IF NOT EXISTS ( SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'" + DestinationTbl
+ @"') AND name = '" + columnList[i]
+ @"' ) BEGIN "
+ alterTableSQL
+ @" END ";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.